Languages
[Edit]
EN

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

5 points
Created by:
Kate_C
2854

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

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍