Format DateTime TimeZone

StewartBW 1,905 Reputation points
2024-07-31T12:28:06.6266667+00:00

Hello

This code will return -04:00 while my time zone is set to -05:00

Also, to show the -05:00 like -0500 is there a specific formatter?

Thanks :)

DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz")
Developer technologies | VB
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2024-07-31T12:40:31.6933333+00:00

    Hi,@ StewartBW. Welcome to Microsoft Q&A.

    In VB, you could achieve this by using DateTimeOffset to ensure that the correct time zone offset is used and then format the output string accordingly.

    
    
          Dim now As DateTimeOffset = DateTimeOffset.Now
    
          Dim formattedDate As String = now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz")
    
          ' Replace the colon in the time zone offset with nothing
    
          formattedDate = formattedDate.Replace(":", "")
    
          Console.WriteLine(formattedDate)
    
    
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most 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.