# #C Problem- - - - Time collection

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

## 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 on other sites
I think that you are looking for:
System.DateTime.Now

##### Share on other sites
Oops, double post...

##### Share on other sites
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 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: 19The current minute is: 7The current second is: 10The 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 on other sites
Quote:
 Original post by kSquaredUse 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 :).

• 10
• 17
• 9
• 13
• 41