Languages
[Edit]
EN

C# / .NET - get current year, month, day, hour, minute, second, millisecond

5 points
Created by:
Kate_C
19810

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

References

  1. String.Format Method - Microsoft Docs
  2. Coordinated Universal Time (UTC) - Wikipedia
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join