Current Universal time in classic asp

空扰寡人 提交于 2019-12-18 09:36:26

问题


How can I get the current time as Universal time in classic asp. I know how to get in C# and I am getting the universal time in c# with following line ((DateTime.Now).ToUniversalTime()).ToString("s") and this code gives me time like this 2012-07-09T10:29:49

But I want to know the equivalent in classic asp. Thanks


回答1:


According to u229.no, there isn't any way for ASP Classic to convert to UTC.

Take a look at the code the author provided below.

The GetServerGMT routine will return something like: Wed, 01 Feb 2006 15:21:59 UTC.

Function GetServerGMT()

    // Use JScript to get the current GMT time stamp and store it in Session("ServerGMT")
    Server.Execute "GetServerGMT.asp"
    GetServerGMT = Session("ServerGMT")

End Function 

And this is how the GetServerGMT.asp file with the jscript code looks like:

<%@language="jscript"%>
<%
    var od = new Date();
    var nd = od.toUTCString();
    Session("ServerGMT") = nd;
%> 

There are other jscript methods that you can use as well.




回答2:


As Ian pointed out you can generate a UTC time via Javascript.

You specified "ASP Classic" which of course includes Javascript as a language, so there's one answer for you: Call (new Date()).toUTCString().

If by chance you prefer to code your pages in mostly VBScript, you can mix in just a little Javascript to get it done. You don't need to resort to Server.Execute or Sessions to make that happen.

This works for me:

<%@ language="VBScript" %>
<script language='Javascript' runat='server'>
  function jsGetUTCTime() {
    var d = new Date();
    return d.toUTCString();
  }
</script>
<script language='VBScript' runat='server'>
Function getUTCTime()
    ' Use JScript to get the current GMT time stamp
    getUTCTime = jsGetUTCTime()
End Function
</script>
<!DOCTYPE html>
<html>
  <head>
    <title>Mix</title>
  </head>
  <body>
   <h2>The time is:</h2>
   <%= getUTCTime() %>
  </body>
</html>



回答3:


Variation on above code

<%@ language="VBScript" %>
<!DOCTYPE html>
<html>
        <head>
        <script language='Javascript' runat='server'>

            // good for creating cookie timestamps that match RFC 1123
            function jsGetFutureTimeGMT(minutes) {
                var d = new Date();
                var future = new Date();
                future.setTime(d.getTime() + (minutes*60000));
                var utcString = future.toUTCString();
                var tz = utcString.indexOf("UTC");
                if (tz > 0) {
                    utcString = utcString.substring(0, tz) + "GMT";
                }
                return utcString;
            }
        </script>

        <title>ASP Classic RFC 1123 compliant GMT timestamp</title>
    </head>
    <body>
    <h2>ASP Classic RFC 1123 compliant GMT timestamp</h2>
    <pre>
     The VBS time right now is: <%= Now() %>
     The time in 10 minutes is: <%= jsGetFutureTimeGMT(10) %>
    </pre>
    </body>
</html>



回答4:


If your application is connected to a database, you can probably get a timestamp from the database with a simple fast query. For example, in MySQL it's: SELECT unix_timestamp().

This will give you a Unix Timestamp (seconds since the Unix Epoch). From there convert into whatever format you like.




回答5:


An alternative approach is

%>
 <script runat="server" language="javascript">
     function getTimezoneOffset() {             
         return (new Date()).getTimezoneOffset()
     }
</script>
<%
Function nowUtc()
   nowUtc=DateAdd("n",getTimezoneOffset(),Now)
End Function


来源:https://stackoverflow.com/questions/11398084/current-universal-time-in-classic-asp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!