locked
How can I show hour, min, sec with DateTime?

    Question

  • Hi,

    I am trying:

      DateTime date = DateTime.Now.Date;
      System.Diagnostics.Debug.WriteLine("DateTime date:" + date.ToString("M/d/yyyy h:mm:ss tt"));

    and I got:

    DateTime date:8/11/2014 12:00:00 AM

    no hours, min, sec.

    What I am doing wrong.

    Thanks


    ADRIAN DIBU

    Monday, August 11, 2014 1:50 PM

Answers

  • You are calling the ToString method on the DateTime.Now.Date object. If you need the hours and seconds, you should call it on DateTime.Now:

    DateTime date = DateTime.Now;
       System.Diagnostics.Debug.WriteLine("DateTime date:" + date.ToString("M/d/yyyy h:mm:ss tt"));
    
    The Date property gets only the date component of the DateTime instance: http://msdn.microsoft.com/en-us/library/system.datetime.date(v=vs.110).aspx
    • Marked as answer by adibu Monday, August 11, 2014 2:53 PM
    Monday, August 11, 2014 2:06 PM