Can anyone help me with this conversion? I am not an asp classic developer so simplest method would work best.

I need to have the date formatted as 2014-04-09 12:12:52 -01:00

In PHP i can do it using date('Y-m-d H:i:s P');

Any ideas?

有帮助吗?

解决方案

Try this (but see "Slight Caveat" at the bottom of the answer);

'Function used to pad numeric values, i.e 1 to 01
'i_zeros - Number of zero's to prefix the s_value with.
Function format_zeros(s_value,i_zeros)
  If Len(s_value) < i_zeros Then
    Do Until Len(s_value) = i_zeros
      s_value = "0" & s_value
    Loop
  End If
  format_zeros = s_value
End Function

Dim somedate, sometime, somedatetimestamp

'Build the date string in the format yyyy-mm-dd
somedate = Year(Date()) & "-" & format_zeros(Month(Date()), 2) & "-" & format_zeros(Day(Date()), 2)
'Build the time string in the format hh:mm:ss
sometime = format_zeros(Hour(Time()), 2) & ":" & format_zeros(Minute(Time()), 2) & ":" & format_zeros(Second(Time()), 2)
'Concatenate both together to build the timestamp yyyy-mm-dd hh:mm:ss
somedatetimestamp = somedate & " " & sometime

Call Response.Write(somedatetimestamp)

NOTE:

I've already written an extensive answer that addresses date formatting here and explains the various functions available to help you build custom formats.

Slight Caveat

Not sure how to best approach the timezone offset but there is some decent articles like this one that explain conversion between timezones.

If you're dealing with -0100 timezone offset exclusively you can just append it to your string something like;

somedatetimestamp = somedatetimestamp & " -0100"

As for conversion to and from a timezone that can be decidedly trickier.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top