locked
DateTime.ToString("h") not working RRS feed

  • Question

  • User379080138 posted
    I have a situation in which I want to get the hour component of a DateTime, but I want the 12-hour value that would go with an AM/PM. The only way I could find to do this was to use a date format string. Since h is the format character used for this, I figured I would use the method: ..ToString("h") However, this would give me an error. But when I used it with a space and trimmed it: ..ToString("h ").Trim() It would work fine. Why am I not allowed to use "h" as a format string? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
    Wednesday, September 29, 2010 1:20 PM

Answers

All replies

  • User1224194097 posted

    use 0:t to get Time in 12Hr format

    string strTime = DateTime.Now.ToShortTimeString();
    string strTime1 = DateTime.Parse("09/12/2010 22:23").ToShortTimeString();
    string strTime2 = string.Format("{0:t}", DateTime.Parse("09/12/2010 22:23"));



    Wednesday, September 29, 2010 1:51 PM
  • User379080138 posted
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META content=text/html;charset=iso-8859-1 http-equiv=Content-Type> <META name=GENERATOR content="MSHTML 8.00.7600.16625"></HEAD> <BODY style="PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: 15px" id=MailContainerBody leftMargin=0 topMargin=0 CanvasTabStop="true" name="Compose message area">
    I think you misunderstood what I wanted. I did not want a time formatted in a 12hr format, I want the hour as a number from 1-12. I did not want the minutes, AM/PM, or anything else, just the hour.
    --
    Nathan Sokalski
    njsokalski@hotmail.com
    http://www.nathansokalski.com/
    "sansan" wrote in message news:4104137@forums.asp.net...

    use 0:t to get Time in 12Hr format

    string strTime = DateTime.Now.ToShortTimeString();
    string strTime1 = DateTime.Parse("09/12/2010 22:23").ToShortTimeString();
    string strTime2 = string.Format("{0:t}", DateTime.Parse("09/12/2010 22:23"));




    Santhosh
    Life revolves in circular motion and ultimately reaches a point called FRUSTRATION!!!!! for every revolution

    http://forums.asp.net/p/1607824/4104137.aspx#4104137

    </BODY></HTML>
    Wednesday, September 29, 2010 2:16 PM
  • User1224194097 posted

    Ok, you have to use hh instead of H or HH to get 12HR format

    DateTime.Now.ToString("hh")
    //Or a Date Time value
    // Substitute your value.
    DateTime.Parse("09/12/2010 22:23").ToString("hh")
    Wednesday, September 29, 2010 3:46 PM
  • User379080138 posted
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META content=text/html;charset=iso-8859-1 http-equiv=Content-Type> <META name=GENERATOR content="MSHTML 8.00.7600.16625"></HEAD> <BODY style="PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: 15px" id=MailContainerBody leftMargin=0 topMargin=0 CanvasTabStop="true" name="Compose message area">
    I know I'm probably starting to sound like a nitpicker here, but doesn't "hh" give the leading 0? I know that I could just use the .TrimStart("0"c) function to get rid of the leading 0, but if I need to do that, I may as well just do what I said I did in my original post, which was use .ToString("h ").Trim(). I am looking for a way to get the hour, in 12hr format, with the minimum number of digits (no leading zeros), using only a format string. If the "h" format string cannot be used by itself, why is that not mentioned in the documentation?
    --
    Nathan Sokalski
    njsokalski@hotmail.com
    http://www.nathansokalski.com/
    "sansan" wrote in message news:4104326@forums.asp.net...

    Ok, you have to use hh instead of H or HH to get 12HR format

    DateTime.Now.ToString("hh")
    //Or a Date Time value
    // Substitute your value.
    DateTime.Parse("09/12/2010 22:23").ToString("hh")

    Santhosh
    Life revolves in circular motion and ultimately reaches a point called FRUSTRATION!!!!! for every revolution

    http://forums.asp.net/p/1607824/4104326.aspx#4104326

    </BODY></HTML>
    Wednesday, September 29, 2010 4:23 PM
  • User188494879 posted

    You could see various formats here and construct your accordingly:

    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

    http://blog.stevex.net/string-formatting-in-csharp/

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 29, 2010 4:29 PM
  • User1224194097 posted

    Ok, use %h for that.

    //Custom Date Time Value
    String.Format("{0:%h}", DateTime.Parse("09/12/2010 04:23"))
    //Current Time
    DateTime.Now.ToString("%h")

    That should strip off leading zeros.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 29, 2010 4:33 PM