Format DateTime TimeZone

StewartBW 1,145 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")
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,740 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    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 Answers by the question author, which helps users to know the answer solved the author's problem.