EN
C# / .NET - get current year, month, day, hour, minute, second, millisecond
5
points
In C# / .NET DateTime
structure allows to take current date and time.
1. Current time example
DateTime now = DateTime.Now;
int hour = now.Hour;
int minute = now.Minute;
int second = now.Second;
int millisecond = now.Millisecond;
//Console.WriteLine(hour + ":" + minute + ":" + second + "." + millisecond);
Console.WriteLine(String.Format("{0:00}:{1:00}:{2:00}.{3:000}", hour, minute, second, millisecond));
Output:
20:17:06.064
2. Current date example
DateTime now = DateTime.Now;
int year = now.Year;
int month = now.Month;
int day = now.Day;
//Console.WriteLine(day + "-" + month + "-" + year);
Console.WriteLine(String.Format("{0:00}-{1:00}-{2:0000}", day, month, year));
Output:
13-08-2019
3. Current UTC time example
DateTime now = DateTime.UtcNow;
int hour = now.Hour;
int minute = now.Minute;
int second = now.Second;
int millisecond = now.Millisecond;
//Console.WriteLine(hour + ":" + minute + ":" + second + "." + millisecond);
Console.WriteLine(String.Format("{0:00}:{1:00}:{2:00}.{3:000}", hour, minute, second, millisecond));
Output:
19:17:06.064
4. Current UTC date example
DateTime now = DateTime.UtcNow;
int year = now.Year;
int month = now.Month;
int day = now.Day;
//Console.WriteLine(day + "-" + month + "-" + year);
Console.WriteLine(String.Format("{0:00}-{1:00}-{2:0000}", day, month, year));
Output:
13-08-2019