Reputation: 87
I wish to construct a DateTime
which looks like below in Visual Basics:
Wed Aug 23 2017 10:00:00 GMT+0530
Can anyone please suggest how to construct a DateTime
info where timezone info could also be shown as above?
Upvotes: 0
Views: 11146
Reputation: 25013
A DateTime has a Kind which is one of DateTimeKind.Local, DateTimeKind.Utc, or DateTimeKind.Unspecified, so although it has some time zone information, it is too limited to specify another offset.
A DateTimeOffset can hold other offsets, so it may suit your purposes better:
Module Module1
Sub Main()
Dim hereAndNow = DateTime.Now
Dim utc = DateTime.UtcNow
Dim noTzi = Date.SpecifyKind(utc, DateTimeKind.Unspecified)
Dim inIndia As New DateTimeOffset(noTzi, TimeSpan.FromHours(5.5))
Console.WriteLine(hereAndNow.ToString("yyyy-MM-dd HH:mm:ss K"))
Console.WriteLine(utc.ToString("yyyy-MM-dd HH:mm:ss K"))
Console.WriteLine(inIndia.ToString("yyyy-MM-dd HH:mm:ss 'GMT' K"))
Console.ReadLine()
End Sub
End Module
Outputs:
2017-08-23 11:36:55 +01:00
2017-08-23 10:36:55 Z
2017-08-23 10:36:55 GMT +05:30
I had to set the Kind of the DateTime variable utc
to DateTimeKind.Unspecified
to be able to use it in the DateTimeOffset constructor.
You should use the K custom format specifier for displaying time zone information.
Upvotes: 0
Reputation: 1883
You can do something like below...
Dim dt as DateTime = DateTime.UtcNow
Dim output as String = dt.ToLocalTime().ToString("MMM dd, yyyy HH:mm:ss tt ""GMT"" zzz")
Console.WriteLine(output) 'Outputs Aug 23, 2017 11:16:29 AM GMT +01:00
See Custom Date and Time Format Strings for more information.
Upvotes: 2