Share via

Formatted date time

StewartBW 1,905 Reputation points
2024-07-31T11:24:39.53+00:00

Hello all,To get the date time in this format, is there a formatter or I need to populate it part by part?

Wed, 31 Jul 2024 05:09:03 -0500

Thanks :)

Developer technologies | VB
0 comments No comments

Answer accepted by question author

  1. Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
    2024-08-02T01:31:40.3266667+00:00

    Hi @StewartBW The behavior you are observing is likely due to the distinction between Standard Time and Daylight Saving Time. Eastern Standard Time (EST) has a UTC offset of -5, while Eastern Daylight Time (EDT), which is observed during the summer months, has a UTC offset of -4.

    When you run the code during the period when Daylight Saving Time is in effect, the UTC offset will be -4, which corresponds to Eastern Daylight Time (EDT).

    You may need to manually change the offset to EST.

          Dim estZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
            
            Dim currentDate As DateTime = DateTime.Now
            
            Dim estTime As DateTime = TimeZoneInfo.ConvertTime(currentDate, TimeZoneInfo.Local, estZone)
            
            Dim estOffset As TimeSpan = estZone.BaseUtcOffset
            
            Dim formattedDate As String = estTime.ToString("ddd, dd MMM yyyy HH:mm:ss") & " " & estOffset.ToString("hh\:mm")
            
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. StewartBW 1,905 Reputation points
    2024-07-31T15:04:16.0666667+00:00

    utclocal

    Was this answer helpful?


  2. Marcin Policht 89,810 Reputation points MVP Volunteer Moderator
    2024-07-31T11:29:03.94+00:00

    Use the following

    Dim currentDate As DateTime = DateTime.Now
    Dim formattedDate As String = currentDate.ToString("ddd, dd MMM yyyy HH:mm:ss zzz")
    Console.WriteLine(formattedDate)
    
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.