tags:

views:

12

answers:

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 ?

A: 

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
If I do dt.ToLocalTime it gives me "09/27/2010 04:54:28" which is coorect but without am or PM
cshah
@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