Languages
[Edit]
EN

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

5 points
Created by:
Zeeshan-Peel
730

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join