How to format datetime with minimal delimiters and timezone in VBScript?

I have the following code in C #:

DateTime dt = GetDateTime(); string formatted = dt.ToString("yyyyMMddTHHmmsszz"); 

which returns a date in the following format:

 20100806T112917+01 

I would like to be able to get the same results in VBScript (for an outdated ASP application). It is especially important that I get information about the UTC offset or change the time to UTC.

How to do it?

+4
source share
2 answers

To format the date, I like to use the .NET StringBuilder class from VBScript:

 Option Explicit Dim sb : Set sb = CreateObject("System.Text.StringBuilder") sb.AppendFormat "{0:yyyyMMddTHHmmsszz}", Now() Response.Write sb.ToString() 

The above:

 20100806T201139-07 

It is assumed that .NET is installed on your web server.

+7
source

Here is my own attempt. Best decisions will be kindly made!

 Function ToDateTimeStringMinimalSeparators(dateTime) ' -------------------------------------------------------------------------- ' FORMATTHEUTCOFFSET ' -------------------------------------------------------------------------- Dim oShell, activeTimeBias Set oShell = CreateObject("WScript.Shell") activeTimeBias = oShell.RegRead("HKEY_LOCAL_MACHINE\System\" & _ "CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias") Dim sign sign = "-" If activeTimeBias < 0 Then sign = "+" ' Make it positive while we're doing calculations activeTimeBias = activeTimeBias * -1 End If Dim atbHours, atbMins atbHours = Right("00" & Int(activeTimeBias / 60), 2) atbMins = Right("00" & (activeTimeBias Mod 60), 2) If atbMins = "00" Then atbMins = "" End If Dim utcOffset utcOffset = sign & atbHours & atbMins ' -------------------------------------------------------------------------- ' FORMATTHEMAINDATE ' -------------------------------------------------------------------------- Dim dateBody dateBody = Right("0000" & Year(dateTime), 4) & _ Right("00" & Month(dateTime), 2) & _ Right("00" & Day(dateTime), 2) & _ "T" & _ Right("00" & Hour(dateTime), 2) & _ Right("00" & Minute(dateTime), 2) & _ Right("00" & Second(dateTime), 2) ToDateTimeStringMinimalSeparators = dateBody & utcOffset End Function 
+2
source

All Articles