javascript - 在经典 ASP 中将日期转换为 GMT

标签 javascript date asp-classic

我想将以下日期转换为 GMT 日期。请任何人都可以告诉我该怎么做。任何帮助将不胜感激。

更新

嘿,我发现了这个

   <% response.write currentUTC() %>

   <script language=jscript runat=server>
        function currentUTC(){
        var d, s;
       d = new Date();
       s = "Server current UTC time is: ";
       s += d.toUTCString('!%a, %d %b %Y %H:%M:%S GMT');
       return(s);
      }
 </script>

它输出:服务器当前 UTC 时间是:2016 年 1 月 15 日星期五 07:42:13 UTC

但我需要这种格式:YYYYMMDDHHMMSS

请问有人吗?

更新

我尝试使用以下功能:

GetServerGMT=Year(Now())&Month(Now())&Day(Now())&Hour(Now())&Minute(Now())&Second(Now())&WeekDay(Now()) 

输出:20161172035121

但这不是有效的时间戳。

最佳答案

添加到 Lankymart 链接的内容。 ASP Classic 有许多时间/日期选项,根据我的经验,您似乎必须为每种不同类型创建一个函数或子函数。

我的 HTTP Only cookie GMT 与我的 GMT RSS Feed 布局不同。

示例 1:strGMTDateRFC22 = CookieServerUTC("d",1,5,"GMT")

'# following formating RFC22 for your GMT Cookie time. 
    strGMTDateRFC22 = CookieServerUTC("d","&strCookieExpires&",5,"GMT")  ' 1 Day set in char enc dec page
    Response.AddHeader "Set-Cookie", strCookieName & "=" & strCookieKey & "=" & strCookieValue & "; expires=" & strGMTDateRFC22 & "; domain="& strCookieDomain &"; path=/; HTTPOnly"

两个函数中的第一个

    Function CookieServerUTC(strD,strT,strOSet,strZ)
Dim strTG,strCookieD
'snipped unwanted code
        strTG = DateAdd("h", strOSet,Now())
        strCookieD = DateAdd(strD,strT,CDate(strTG))
     CookieServerUTC =  fncFmtDate(strCookieD, "%a, %d %b %Y %H:%N:%S "&strZ&"")

    End Function

另一个需要设置服务器 UTC 的示例 这允许 strH = "h"strT = "5"(strT 时间偏移 +/-)和 strZ GMT(时区)

Function GetServerUTC(strH,strT,strZ)

 GetServerUTC = fncFmtDate(DateAdd(strH,strT,Now()), "%a, %d %b %Y %H:%N:%S "&strZ&"")

End Function

然后是 2001 年发布的函数脚本,当时 ASP Classic 还很热门。 4guysfromrolla.com 发布了它,我可以想象它已经帮助了许多时间日期格式爱好者。

这是链接和打击代码,以防 2001 从互联网上删除。 Customizable Date Formatting Routine by Ken Schaefer

    Function fncFmtDate( _
        byVal strDate, _
        byRef strFormat _
       )
     ' Accepts strDate as a valid date/time,
     ' strFormat as the output template.
     ' The function finds each item in the
     ' template and replaces it with the
     ' relevant information extracted from strDate

     ' Template items (example)
     ' %m Month as a decimal (02)
     ' %B Full month name (February)
     ' %b Abbreviated month name (Feb )
     ' %d Day of the month (23)
     ' %O Ordinal of day of month (eg st or rd or nd)
     ' %j Day of the year (54)
     ' %Y Year with century (1998)
     ' %y Year without century (98)
     ' %w Weekday as integer (0 is Sunday)
     ' %a Abbreviated day name (Fri)
     ' %A Weekday Name (Friday)
     ' %H Hour in 24 hour format (24)
     ' %h Hour in 12 hour format (12)
     ' %N Minute as an integer (01)
     ' %n Minute as optional if minute <> 0
     ' %S Second as an integer (55)
     ' %P AM/PM Indicator (PM)
'#### READ THE FORMATTING GUIDE ABOVE #####

'Snipped the code due to the 1% possibility you didn't format your date / time correctly and the code tosses an error based on that formatting.

    End Function ' fncFmtDate

如您所见,我们有许多可能的解决方案。 找到适合您的项目并在此基础上构建的日期时间格式化解决方案。

Ken 的代码确实需要特定的输入日期格式。 对于那些不真正喜欢调试或创建自己的代码的人,您可以使用我从上面 Ken 的示例中构建的代码。

这适用于 ASP Classic 中的任何格式并发挥其神奇作用。 请注意,代码的第一部分纠正了单位数问题。 如果您曾经使用过货币格式,您应该了解这一点。

Murray 的 Active DateFormat to Anything Code 重新设计了 Ken 开始的工作。 这是神奇的部分。

 if (DatePart("m", strDate) < 10) then
    twoDigMonth = "0" & DatePart("m", strDate)
 else
    twoDigMonth = DatePart("m", strDate)
 end if

 if (DatePart("d", strDate) < 10) then
    twoDigDay = "0" & DatePart("d", strDate)
 else
    twoDigDay = DatePart("d", strDate)
 end if

然后这是其余的代码。 删除所有“On Error Resume Next”,因为只要您的输入得到纠正,就不需要它们。

 ' Insert Month Numbers
 strFormat = Replace(strFormat, "%m", _
          DatePart("m", strDate), 1, -1, vbBinaryCompare)

 ' Insert Month Numbers
 strFormat = Replace(strFormat, "%M", _
          twoDigMonth, 1, -1, vbBinaryCompare)

 ' Insert non-Abbreviated Month Names
 strFormat = Replace(strFormat, "%B", _
          MonthName(DatePart("m", strDate), _
          False), 1, -1, vbBinaryCompare)

 ' Insert Abbreviated Month Names
 strFormat = Replace(strFormat, "%b", _
          MonthName(DatePart("m", strDate), _
          True), 1, -1, vbBinaryCompare)

 ' Insert Day Of Month
 strFormat = Replace(strFormat, "%d", _
          DatePart("d",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Day Of Month
 strFormat = Replace(strFormat, "%D", _
          twoDigDay, 1, _
          -1, vbBinaryCompare)

 ' Insert Day of Month Ordinal (eg st, th, or rd)
 strFormat = Replace(strFormat, "%O", _
          fncGetDayOrdinal(Day(strDate)), _
          1, -1, vbBinaryCompare)

 ' Insert Day of Year
 strFormat = Replace(strFormat, "%j", _
          DatePart("y",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Long Year (4 digit)
 strFormat = Replace(strFormat, "%Y", _
          DatePart("yyyy",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Short Year (2 digit)
 strFormat = Replace(strFormat, "%y", _
          Right(DatePart("yyyy",strDate),2), _
          1, -1, vbBinaryCompare)

 ' Insert Weekday as Integer (eg 0 = Sunday)
 strFormat = Replace(strFormat, "%w", _
          DatePart("w",strDate,1), 1, _
          -1, vbBinaryCompare)

 ' Insert Abbreviated Weekday Name (eg Sun)
 strFormat = Replace(strFormat, "%a", _
          WeekDayName(DatePart("w",strDate,1),True), 1, _
          -1, vbBinaryCompare)

 ' Insert non-Abbreviated Weekday Name
 strFormat = Replace(strFormat, "%A", _
          WeekDayName(DatePart("w",strDate,1),False), 1, _
          -1, vbBinaryCompare)

 ' Insert Hour in 24hr format
 str24HourPart = DatePart("h",strDate)
 If Len(str24HourPart) < 2 then str24HourPart = "0" & _
                                                 str24HourPart
 strFormat = Replace(strFormat, "%H", str24HourPart, 1, _
          -1, vbBinaryCompare)

 ' Insert Hour in 12hr format
 int12HourPart = DatePart("h",strDate) Mod 12
 If int12HourPart = 0 then int12HourPart = 12
 strFormat = Replace(strFormat, "%h", int12HourPart, 1, _
          -1, vbBinaryCompare)

 ' Insert Minutes
 strMinutePart = DatePart("n",strDate)
 If Len(strMinutePart) < 2 then _
          strMinutePart = "0" & strMinutePart
 strFormat = Replace(strFormat, "%N", strMinutePart, _
          1, -1, vbBinaryCompare)

 ' Insert Optional Minutes
 If CInt(strMinutePart) = 0 then
  strFormat = Replace(strFormat, "%n", "", 1, _
           -1, vbBinaryCompare)
 Else
  If CInt(strMinutePart) < 10 then _
           strMinutePart = "0" & strMinutePart
  strMinutePart = ":" & strMinutePart
  strFormat = Replace(strFormat, "%n", strMinutePart, _
           1, -1, vbBinaryCompare)
 End if

 ' Insert Seconds
 strSecondPart = DatePart("s",strDate)
 If Len(strSecondPart) < 2 then _
          strSecondPart = "0" & strSecondPart
 strFormat = Replace(strFormat, "%S", strSecondPart, 1, _
          -1, vbBinaryCompare)

 ' Insert AM/PM indicator
 If DatePart("h",strDate) >= 12 then
   strAMPM = "PM"
 Else
   strAMPM = "AM"
 End If

 strFormat = Replace(strFormat, "%P", strAMPM, 1, _
          -1, vbBinaryCompare)


 fncFmtDate = strFormat

将这一切捆绑到一个很好的函数调用中。

Function fncFmtDate(strDate, strFormat)
'Example for your perfect GMT time in Sitemaps, RSSFeeds, Google Stuff, Cookies
'# I'm in Louisiana so I'm -6 hours from GMT 
'# fncFmtDate(DateAdd("h", -6,Now()), "%a, %d %b %H:%N:%S GMT")&"

'# Place code above here.... Don't forget the Magic part.

End Function

现在你已经看到了,100% 可用的实时代码。 如果您看到错误,则这是您的输入而不是函数。请务必遵循我列出的 GMT 格式示例。

我们无法为每个 ASP Classic 程序员提供完美的解决方案,我们会提供对我们有用的内容和基本的代码实践。
如果我认为这都是关于复制和粘贴解决方案的,我会从 80 年代开始就发布了。 给它时间,处理错误陷阱并学会从左到右、从上到下阅读。

Solution to format placed here: Issue: It outputs : Server current UTC time is: Fri, 15 Jan 2016 07:42:13 UTC But I need in this format: YYYYMMDDHHMMSS

以下是如何使用该函数来获取格式。看看我如何将 %a 更改为 %Y ?说明在代码中。

fncFmtDate(DateAdd("h", -6,Now()), "%Y, %m %d %H:%N:%S GMT")

如果不需要空格或逗号,请将其删除。但是你如何显示你想要的格式是我在ASP开发中没有见过的。

fncFmtDate(DateAdd("h", -6,Now()), ""%Y%m%d%H%N%S GMT")

使用代码,告诉它你想要什么作为输出,阅读代码。

关于javascript - 在经典 ASP 中将日期转换为 GMT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34741762/

相关文章:

html - 生成的 HTML word 文档无法正确显示图像

javascript - 在 Component 内部使用react,为什么我们需要使用 "this."来引用函数

javascript - 使用#anchors 移动带有额外定位的滚动条

php - jQuery datepicker,选择日期后添加一定天数

java - 使用 SimpleDateFormat 作为文件名字符串

xml - 如何在经典 ASP 中使用地理编码 API v3

asp-classic - ASP 脚本未运行

javascript - 通过javascript中的onclick方法更改类元素中的显示

javascript - 如何使用jquery连续垂直滚动图像

java - 如何格式化此字符串 "2015-06-22T09:40:30+01:00"in dd/MM/yyyy at hh :mm format?