How do you get the current time of day?

后端 未结 20 2517
长发绾君心
长发绾君心 2020-11-28 18:40

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

Example: 5:42:12 PM

相关标签:
20条回答
  • 2020-11-28 19:18

    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)
    
    0 讨论(0)
  • 2020-11-28 19:18

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

    0 讨论(0)
  • 2020-11-28 19:18
    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...

    0 讨论(0)
  • 2020-11-28 19:21

    Use the code below

    DateTime.Now.ToString("h:mm:ss tt")
    
    0 讨论(0)
  • 2020-11-28 19:21

    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.

    0 讨论(0)
  • 2020-11-28 19:26

    Try this:

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

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

    0 讨论(0)
提交回复
热议问题