Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

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

0 contributions
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

References

  1. String.Format Method - Microsoft Docs
  2. Coordinated Universal Time (UTC) - Wikipedia
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more