Jump to content
  • Advertisement
Sign in to follow this  
jrblkbelt

#C Problem- - - - Time collection

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so I am trying to get the system's time (hours: Minutes: seconds) and I have found some code that I thought would do the trick but then i realised it was stupid and actually assigning the numbers to each thing. Heres that code:
System.DateTime moment = new System.DateTime(1999, 1, 13, 3, 57, 32, 11);
// Year gets 1999.
int year = moment.Year;

// Month gets 1 (January).
int month = moment.Month;

// Day gets 13.
int day = moment.Day;

// Hour gets 3.
int hour = moment.Hour;

// Minute gets 57.
int minute = moment.Minute;

// Second gets 32.
int second = moment.Second;

// Millisecond gets 11.
int millisecond = moment.Millisecond;
I need something similar, but should have exactly what the system time is. I am new to the whole programming thing so please don't flame me or whatever.

Share this post


Link to post
Share on other sites
Advertisement
I have no idea how I would use that... can you show me an example of how to get the hours out of that? Then I can do the same for the minutes, ect.


[EDIT]

I also found this little code snipit on Microsoft's website:

public int Hour { get; }

but if I put that code in it screws the rest if the code up, I think because of the { }.

[EDIT (again)]

Yea, I got that to work but I need the hour minutes and seconds seperated into seperate fields so that I can edit each piece individually.

Share this post


Link to post
Share on other sites
Use the Hour, Minute, and Second properties (among others) to get what you want. For instance,
DateTime dt = DateTime.Now;

Console.WriteLine("The current hour is: {0}", dt.Hour);
Console.WriteLine("The current minute is: {0}", dt.Minute);
Console.WriteLine("The current second is: {0}", dt.Second);

Console.WriteLine("The current time is: {0}", dt);
prints
The current hour is: 19
The current minute is: 7
The current second is: 10
The current time is: 2006/03/30 19:07:10
on my machine.

If you just want to print the current system time, you can use the format specifiers for the DateTime.ToString() method. For instance, Console.WriteLine("The current system time is: {0}", dt.ToString("hh:mm:ss tt")); prints the text The current system time is: 7:07:10 PM in the above example.

Hope that helps,

Share this post


Link to post
Share on other sites
Quote:
Original post by kSquared
Use the Hour, Minute, and Second properties (among others) to get what you want. For instance,

DateTime dt = DateTime.Now;

Console.WriteLine("The current hour is: {0}", dt.Hour);
Console.WriteLine("The current minute is: {0}", dt.Minute);
Console.WriteLine("The current second is: {0}", dt.Second);


Thanks, thats exactly what I am looking for! Your a life saver :).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!