Link to home
Start Free TrialLog in
Avatar of skij
skijFlag for Canada

asked on

ASP.NET/VB: Convert Date and Time to YYYY-MM-DDTHH:MM:SS

Using ASP.NET/VB, how can I convert a string from this format:
5/25/2015 3:45:27 PM
to this format:
2015-05-25T14:59:04

I want the time format to match the W3C Datetime specification.
https://www.w3.org/TR/NOTE-datetime
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skij

ASKER

Thanks.  How can I incorporate that idea into my code?

Imports System.Web
Imports System.Globalization

Partial Class _Default

    Inherits CMSBasePage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim TS as String

    TS = "5/25/2015 3:45:27 PM"

    Response.Write( DateTimeW3C(TS) )
    Response.End()

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dt As DateTime
        Dim provider As CultureInfo = CultureInfo.InvariantCulture
        Dim datestring As String = "M/d/yyyy h:mm:ss tt"
        dt = Date.ParseExact(TextBox1.Text, datestring, provider)
        Label1.Text = dt.ToString("yyyy-MM-ddTHH:mm:ss")
    End Sub

End Class

Open in new window

Avatar of skij

ASKER

I figured it out, based on your help, thanks.
Imports System.Web
Imports System.Globalization

Partial Class _Default

    Inherits CMSBasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim TS as String

    Response.Write( w3cdate("5/25/2015 3:45:27 PM") )

    Response.End()

    End Sub

    Protected Function w3cdate(TS As String) 
     Dim dt As DateTime
     Dim provider As CultureInfo = CultureInfo.InvariantCulture
     Dim datestring As String = "M/d/yyyy h:mm:ss tt"
     dt = Date.ParseExact(TS, datestring, provider)
     return dt.ToString("yyyy-MM-ddTHH:mm:ss")  
    End Function

End Class

Open in new window