[c#] How do you get the current time of day?

How do you get the current time (not date AND time)?

Example: 5:42:12 PM

This question is related to c# datetime

The answer is


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


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.TimeOfDay returns a TimeSpan and might be what you are looking for.


DateTime.Now.ToString("yyyy-MM-dd h:mm:ss tt");

just Try It's use full to your Need


Datetime.TimeOfDay returns a TimeSpan and might be what you are looking for.


var CurDate= DateTime.Now;
CurDate.Hour;
CurDate.Minute;
CurDate.Millisecond

This will show you only the current time, in 24 hour format:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString().ToString());
        Console.WriteLine(DateTime.Now.ToShortTimeString().ToString());
        Console.ReadLine();
    }
}

Regards
K


DateTime.Now.TimeOfDay

or

DateTime.Now.ToShortTimeString()

I'm experimenting with this also and find these pages helpful as well. First the main class... https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

Now some specifier formats for the ToString method... https://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo(v=vs.110).aspx

Example:

using System;

namespace JD
{
    class Program
    {
        public static DateTime get_UTCNow()
        {
            DateTime UTCNow = DateTime.UtcNow;
            int year = UTCNow.Year;
            int month = UTCNow.Month;
            int day = UTCNow.Day;
            int hour = UTCNow.Hour;
            int min = UTCNow.Minute;
            int sec = UTCNow.Second;
            DateTime datetime = new DateTime(year, month, day, hour, min, sec);
            return datetime;
        }

        static void Main(string[] args)
        {
            DateTime datetime = get_UTCNow();            

            string time_UTC = datetime.TimeOfDay.ToString();
            Console.WriteLine(time_UTC);

            Console.ReadLine();

        }
    }
}

I threw that TimeOfDay method in there just to show that you get a default of 24 hour time as is stated "the time from midnight"

You may use my geter method(); :-D


I think this code will solve your problem

DateTime.Now.ToString("HH:mm")

Get the current date and time, then just use the time portion of it. Look at the possibilities for formatting a date time string in the MSDN docs.


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


var CurDate= DateTime.Now;
CurDate.Hour;
CurDate.Minute;
CurDate.Millisecond

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)

This will show you only the current time, in 24 hour format:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString().ToString());
        Console.WriteLine(DateTime.Now.ToShortTimeString().ToString());
        Console.ReadLine();
    }
}

Regards
K


Another option using String.Format()

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

DateTime.Now.TimeOfDay

or

DateTime.Now.ToShortTimeString()

This can be a possible solution:

DateTime now = DateTime.Now;
string time = now.ToString("T");

Try this one. Its working for me in 3tier Architecture Web Application.

"'" + DateTime.Now.ToString() + "'"

Please remember the Single Quotes in the insert Query.

For example:

string Command = @"Insert Into CONFIG_USERS(smallint_empID,smallint_userID,str_username,str_pwd,str_secquestion,str_secanswer,tinyint_roleID,str_phone,str_email,Dt_createdOn,Dt_modifiedOn) values ("
 + u.Employees + ","
 + u.UserID + ",'"
 + u.Username + "','"
 + u.GetPassword() + "','"
 + u.SecQ + "','"
 + u.SecA + "',"
 + u.RoleID + ",'"
 + u.Phone + "','"
 + u.Email + "','"
 + DateTime.Now.ToString() + "','"
 + DateTime.Now.ToString() + "')";

The DateTime insertion at the end of the line.


very simple DateTime.Now.ToString("hh:mm:ss tt")


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


Get the current date and time, then just use the time portion of it. Look at the possibilities for formatting a date time string in the MSDN docs.


MyEmail.Body = string.Format("The validation is done at {0:HH:mm:ss} Hrs.",DateTime.Now);

Can Use {0:HH:mm:ss}, {0:HH:mm:ss.fff}, {0:DD/mm/yyy HH:mm:ss}, etc...


This will be better, Try This one

    DateTime.Now.ToShortTimeString();

For this you don't need to specify the Format for the Time.


Datetime.TimeOfDay returns a TimeSpan and might be what you are looking for.


Try this:

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

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


This will show you only the current time, in 24 hour format:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString().ToString());
        Console.WriteLine(DateTime.Now.ToShortTimeString().ToString());
        Console.ReadLine();
    }
}

Regards
K


Use the code below

DateTime.Now.ToString("h:mm:ss tt")

DateTime.Now.ToString("yyyy-MM-dd h:mm:ss tt");

just Try It's use full to your Need


I think this code will solve your problem

DateTime.Now.ToString("HH:mm")

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)

This can be a possible solution:

DateTime now = DateTime.Now;
string time = now.ToString("T");

Get the current date and time, then just use the time portion of it. Look at the possibilities for formatting a date time string in the MSDN docs.


To calculate the current datetime:

DateTime theDate = DateTime.UtcNow;

string custom = theDate.ToString("d");

MessageBox.Show(custom);

I'm experimenting with this also and find these pages helpful as well. First the main class... https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

Now some specifier formats for the ToString method... https://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo(v=vs.110).aspx

Example:

using System;

namespace JD
{
    class Program
    {
        public static DateTime get_UTCNow()
        {
            DateTime UTCNow = DateTime.UtcNow;
            int year = UTCNow.Year;
            int month = UTCNow.Month;
            int day = UTCNow.Day;
            int hour = UTCNow.Hour;
            int min = UTCNow.Minute;
            int sec = UTCNow.Second;
            DateTime datetime = new DateTime(year, month, day, hour, min, sec);
            return datetime;
        }

        static void Main(string[] args)
        {
            DateTime datetime = get_UTCNow();            

            string time_UTC = datetime.TimeOfDay.ToString();
            Console.WriteLine(time_UTC);

            Console.ReadLine();

        }
    }
}

I threw that TimeOfDay method in there just to show that you get a default of 24 hour time as is stated "the time from midnight"

You may use my geter method(); :-D


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


Here we go:

 DateTime time = DateTime.Now;
 Console.WriteLine(time.ToString("h:mm:ss tt"));

very simple DateTime.Now.ToString("hh:mm:ss tt")


Try this one. Its working for me in 3tier Architecture Web Application.

"'" + DateTime.Now.ToString() + "'"

Please remember the Single Quotes in the insert Query.

For example:

string Command = @"Insert Into CONFIG_USERS(smallint_empID,smallint_userID,str_username,str_pwd,str_secquestion,str_secanswer,tinyint_roleID,str_phone,str_email,Dt_createdOn,Dt_modifiedOn) values ("
 + u.Employees + ","
 + u.UserID + ",'"
 + u.Username + "','"
 + u.GetPassword() + "','"
 + u.SecQ + "','"
 + u.SecA + "',"
 + u.RoleID + ",'"
 + u.Phone + "','"
 + u.Email + "','"
 + DateTime.Now.ToString() + "','"
 + DateTime.Now.ToString() + "')";

The DateTime insertion at the end of the line.


Get the current date and time, then just use the time portion of it. Look at the possibilities for formatting a date time string in the MSDN docs.


This will show you only the current time, in 24 hour format:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString().ToString());
        Console.WriteLine(DateTime.Now.ToShortTimeString().ToString());
        Console.ReadLine();
    }
}

Regards
K


Try this one. Its working for me in 3tier Architecture Web Application.

"'" + DateTime.Now.ToString() + "'"

Please remember the Single Quotes in the insert Query.

For example:

string Command = @"Insert Into CONFIG_USERS(smallint_empID,smallint_userID,str_username,str_pwd,str_secquestion,str_secanswer,tinyint_roleID,str_phone,str_email,Dt_createdOn,Dt_modifiedOn) values ("
 + u.Employees + ","
 + u.UserID + ",'"
 + u.Username + "','"
 + u.GetPassword() + "','"
 + u.SecQ + "','"
 + u.SecA + "',"
 + u.RoleID + ",'"
 + u.Phone + "','"
 + u.Email + "','"
 + DateTime.Now.ToString() + "','"
 + DateTime.Now.ToString() + "')";

The DateTime insertion at the end of the line.


DateTime.Now.TimeOfDay

or

DateTime.Now.ToShortTimeString()

Try this one. Its working for me in 3tier Architecture Web Application.

"'" + DateTime.Now.ToString() + "'"

Please remember the Single Quotes in the insert Query.

For example:

string Command = @"Insert Into CONFIG_USERS(smallint_empID,smallint_userID,str_username,str_pwd,str_secquestion,str_secanswer,tinyint_roleID,str_phone,str_email,Dt_createdOn,Dt_modifiedOn) values ("
 + u.Employees + ","
 + u.UserID + ",'"
 + u.Username + "','"
 + u.GetPassword() + "','"
 + u.SecQ + "','"
 + u.SecA + "',"
 + u.RoleID + ",'"
 + u.Phone + "','"
 + u.Email + "','"
 + DateTime.Now.ToString() + "','"
 + DateTime.Now.ToString() + "')";

The DateTime insertion at the end of the line.


To calculate the current datetime:

DateTime theDate = DateTime.UtcNow;

string custom = theDate.ToString("d");

MessageBox.Show(custom);

Here we go:

 DateTime time = DateTime.Now;
 Console.WriteLine(time.ToString("h:mm:ss tt"));

Another option using String.Format()

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

MyEmail.Body = string.Format("The validation is done at {0:HH:mm:ss} Hrs.",DateTime.Now);

Can Use {0:HH:mm:ss}, {0:HH:mm:ss.fff}, {0:DD/mm/yyy HH:mm:ss}, etc...


Use the code below

DateTime.Now.ToString("h:mm:ss tt")

Another option using String.Format()

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

This will be better, Try This one

    DateTime.Now.ToShortTimeString();

For this you don't need to specify the Format for the Time.


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()

Another option using String.Format()

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

Try this:

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

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