1

I have a date in VB.net that is stored in ISO 8601 format

'Date here is local time in germany   
Dim s As String = "2010-09-27T16:54:28+02:00"

Dim dt As DateTime
If Date.TryParse(s, dt) = True Then

End If

When I use try to parse it shows me date in my local timezone,

how can I get date as

2010-9-27 4:54 PM as well as GMT DATE ?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
cshah
  • 325
  • 1
  • 4
  • 13

1 Answers1

2

A date is not stored internally with any specific representation.

You need to format it for display, using the correct DateTime format string (either custom or standard):

Dim s As String = "2010-09-27T16:54:28+02:00"

Dim dt As DateTime
If Date.TryParse(s, dt) = True Then
    Dim gmt as String = dt.ToUniversalTime().ToString("r", CultureInfo.InvariantCulture)
    Dim custom as String = dt.ToUniversalTime().ToString("yyyy-M-d h:mm tt", CultureInfo.InvariantCulture)
End If
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • If I do dt.ToLocalTime it gives me "09/27/2010 04:54:28" which is coorect but without am or PM – cshah Sep 30 '10 at 20:49
  • @cshah - `DateTime.ToString` will default to local system settings for its formatting unless defined, so you need to tell it what culture to use. i have updated my answer to reflect this. – Oded Sep 30 '10 at 20:55