Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the current time of day?

Tags:

c#

datetime

People also ask

How to get the current time JavaScript?

Getting the current date and time in JavaScript is quite simple. All you need to do is use the built-in Date object. This code will output the current date and time to the console.

How to get the current time c#?

Using DateTime.ToString() method The DateTime. ToString() method can be used to get the string representation of the current DateTime object in a specified format. Alternatively, we can use the short time t format specifier, which displays the value of the current DateTime object value using a short time pattern.

How to get DateTime time?

How to Get the Current Time with the datetime Module. To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.


DateTime.Now.TimeOfDay gives it to you as a TimeSpan (from midnight).

DateTime.Now.ToString("h:mm:ss tt") gives it to you as a string.

DateTime reference: https://msdn.microsoft.com/en-us/library/system.datetime


Try this:

DateTime.Now.ToString("HH:mm:ss tt");

For other formats, you can check this site: C# DateTime Format


Another option using String.Format()

string.Format("{0:HH:mm:ss tt}", DateTime.Now)

Current time with AM/PM designator:

DateTime.Now.ToString("hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
DateTime.Now.ToString("hh:mm:ss.fff tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)

Current time using 0-23 hour notation:

DateTime.Now.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
DateTime.Now.ToString("HH:mm:ss.fff", System.Globalization.DateTimeFormatInfo.InvariantInfo)

DateTime.Now.TimeOfDay

or

DateTime.Now.ToShortTimeString()