locked
How to get the exact U.S. standard time format from the DateTime.Now string value ? RRS feed

  • Question

  • User-2015880872 posted

    I want to get the exact US standard time value from my DateTime.Now string value. I tried using some different types of text formatting in C# but still unable to get the right one.

    From the codes below I tried something like this:

      //1st attempt
         DateTime dt = DateTime.Now;         
         string today = dt.TimeOfDay.ToString("G", CultureInfo.CreateSpecificCulture("en-us"));
         Label1.Text = today;
        
         //second attempt
         DateTime dt = DateTime.Now;         
         string today = String.Format("{0:t}", dt.TimeOfDay.ToString("G", CultureInfo.CreateSpecificCulture("en-us")));
         Label1.Text = today;
    
         //3rd attempt    
         DateTime dt = DateTime.Now;         
         string today = String.Format("{0:t tt}", dt.TimeOfDay.ToString("G", CultureInfo.CreateSpecificCulture("en-us")));
         Label1.Text = today;

    but I always get this format display 20:14:38.2283911 instead of 8:14 PM or 8:14 AM.
    How do I get this type of time format like 10:15 AM, 8:45 PM, 2:35 PM and so on? Thanks.

    Wednesday, September 10, 2014 8:26 AM

Answers

  • User-434868552 posted

    @timmack 

    Regardless how you display a DateTime value, the DateTime Struct http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx is constant.

    System settings affect the display.

    The safest way to get what YOU want is to concatenate the indilvidual parts.

    DateTime time = DateTime.Now;
    DateTime noon = new DateTime(time.Year, time.Month, time.Day, 12, 0, 0);
    String amPM = " AM";
    if(time >= noon) amPM = " PM"; 
    String timmackTimeUSA =
      time.Hour.ToString() + ":"
    + time.Minute.ToString("0#") + ":"
    + time.Second.ToString("0#")
    + amPM;
    Console.WriteLine(timmackTimeUSA);

    output:  9:51:57 AM

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 10, 2014 9:53 AM
  • User-760709272 posted

    Hi!

     I tried something like this and it seem getting better but there's some little missing. I missed the A.M/P.M. remarks

    DateTime dt = DateTime.Now;
      CultureInfo ci = CultureInfo.InvariantCulture;
       string today = dt.ToString("hh:mm:ss.F", ci);
         Label1.Text = today;

    Using the code above it will display 09:45:49 only instead of 09:45:49 a.m. What did I miss here? Thanks

    Eh?  I posted the code twice now?  You are missing the tt which is AM\PM

    dt.ToString("h:mm:ss tt");

    you then mark someone else's post as the answer? lol

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 10, 2014 10:19 AM

All replies

  • User-760709272 posted

    http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

    DateNow.Now.ToString("h:mm tt")

    Wednesday, September 10, 2014 8:44 AM
  • User-2015880872 posted

    DateNow.Now.ToString("h:mm tt")

    Look, that's the same link I referred to this 

     string dates = dt.ToString("G", CultureInfo.CreateSpecificCulture("en-us"));

    which will give a result of this type of format : 9/10/2014 7:10:21 PM   for example,

    but from this result as an example I just want to query the part of the string which is only 7:10:21 PM.

    The link doesn't demonstrate how to do this method.

    Wednesday, September 10, 2014 9:17 AM
  • User-760709272 posted

    The code I posted does though;

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

    (forgot the "ss" bit :o ) replace DateTime.Now with whatever your date variable is

    Wednesday, September 10, 2014 9:39 AM
  • User-2015880872 posted

    Hi!

     I tried something like this and it seem getting better but there's some little missing. I missed the A.M/P.M. remarks

    DateTime dt = DateTime.Now;
      CultureInfo ci = CultureInfo.InvariantCulture;
       string today = dt.ToString("hh:mm:ss.F", ci);
         Label1.Text = today;

    Using the code above it will display 09:45:49 only instead of 09:45:49 a.m. What did I miss here? Thanks

    Wednesday, September 10, 2014 9:51 AM
  • User-434868552 posted

    @timmack 

    Regardless how you display a DateTime value, the DateTime Struct http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx is constant.

    System settings affect the display.

    The safest way to get what YOU want is to concatenate the indilvidual parts.

    DateTime time = DateTime.Now;
    DateTime noon = new DateTime(time.Year, time.Month, time.Day, 12, 0, 0);
    String amPM = " AM";
    if(time >= noon) amPM = " PM"; 
    String timmackTimeUSA =
      time.Hour.ToString() + ":"
    + time.Minute.ToString("0#") + ":"
    + time.Second.ToString("0#")
    + amPM;
    Console.WriteLine(timmackTimeUSA);

    output:  9:51:57 AM

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 10, 2014 9:53 AM
  • User-760709272 posted

    Hi!

     I tried something like this and it seem getting better but there's some little missing. I missed the A.M/P.M. remarks

    DateTime dt = DateTime.Now;
      CultureInfo ci = CultureInfo.InvariantCulture;
       string today = dt.ToString("hh:mm:ss.F", ci);
         Label1.Text = today;

    Using the code above it will display 09:45:49 only instead of 09:45:49 a.m. What did I miss here? Thanks

    Eh?  I posted the code twice now?  You are missing the tt which is AM\PM

    dt.ToString("h:mm:ss tt");

    you then mark someone else's post as the answer? lol

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 10, 2014 10:19 AM
  • User-2015880872 posted

    Eh?  I posted the code twice now?  You are missing the tt which is AM\PM

    dt.ToString("h:mm:ss tt");

    you then mark someone else's post as the answer? lol

    Thanks dude..You're both right. But the other guy is just more specific. You didn't explain very well at first unlike the other guy. Thanks again to both. Got it now.

    Wednesday, September 10, 2014 10:29 AM