Classic ASP - Server Time vs. Local Time

10,269

Solution 1

EDIT

I tried server side javascript, it returns Central Time too.

Did you mean to say client side JavaScript? You definitely need to use client side script to get the user's device time (not server side script).


You should read this:

Primer on dealing with multiple time zones:

1) Make sure your database server is set to the correct Date/Time in its time zone. Properly account for Daylight Savings Time in its location. Set the server to do this automatically.

2) Create a table in your database with time zones and their offset from UTC (GMT).

3) Always store Now() Date/Time in UTC. Every database vendor should have a UTC Date/Time Now() function (i.e. SYSUTCDATETIME() for SQL Server). This way all times are stored in a universal format agnostic to where the user happens to be sitting. Call Now() from your database, not the client, because mobile devices could be anywhere, but your database server stays in one spot.

4) Have user input their local time zone and store it in your database.

5) When displaying Date/Time stored in UTC back to the user, convert the UTC Date/Time back to the user's time zone using the user's time zone offset. SQL Server makes this a little easier with datetimeoffset.

6) If the user is setting an alarm, have them enter the trigger Date/Time in their local time zone. This way the user can change their local time zone if they move. Also, if time zone rules change you can just fix your time zone table (#2) and then the alarm will still trigger correctly. In your code, to test for alarm trigger, convert trigger time to UTC, and then compare against server time in UTC (i.e. SYSUTCDATETIME()).

7) Daylight Savings Time is tricky! (see 1st link)

Solution 2

In general, Time zone manipulation can't be done directly in classic ASP.

However, if you have full control of the server where the code is running, you can install a COM component written in a language that does have time zone support, then use that component from your classic ASP environment.

For example, you might write the following component in .NET with C#:

using System;
using System.Runtime.InteropServices;

namespace TimeZoneInfoCom
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [Guid("E0C70A94-352D-4C0B-8C2E-8066C88565C5")]
    public class TimeZoneConverter
    {
        public DateTime NowInZone(string timeZoneId)
        {
            return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, timeZoneId);
        }

        public DateTime Convert(DateTime dateTime, string sourceZoneId, string targetZoneId)
        {
            TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById(sourceZoneId);
            TimeZoneInfo targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById(targetZoneId);
            return TimeZoneInfo.ConvertTime(dateTime, sourceTimeZone, targetTimeZone);
        }
    }
}

You would then compile this, copy the DLL to your server, and register it as a COM component (using RegAsm.exe).

Then you could call it in your Classic ASP page, like so:

<html>
<body>

Server Time: <%= Now() %><br>
<br>

<% 
Dim tzconverter
Set tzconverter = Server.CreateObject("TimeZoneInfoCom.TimeZoneConverter")
%>

US Pacific Time:  <%= tzconverter.NowInZone("Pacific Standard Time") %><br>
US Mountain Time:  <%= tzconverter.NowInZone("Mountain Standard Time") %><br>
US Central Time:  <%= tzconverter.NowInZone("Central Standard Time") %><br>
US Eastern Time:  <%= tzconverter.NowInZone("Eastern Standard Time") %><br>
UTC:  <%= tzconverter.NowInZone("UTC") %><br>

<br>

Conversion Example:
<%

Dim originalTime, convertedTime
originalTime = #12/31/2014 00:00:00#
convertedTime = tzconverter.Convert(originalTime, "UTC", "Tokyo Standard Time")
Response.Write(convertedTime)

%>

<%
' Don't forget to destroy the com object!
Set tzconverter = Nothing
%>

</body>
</html>

If you get an "ActiveX component can't create object" error, be sure that you have set "Enable 32-Bit Applications" to True in IIS, under the advanced settings for your application pool.

With regard to SQL Server - If you search, you may find a handful of posts showing ways you can manipulate time in SQL Server, through elaborate stored procedures that either have fixed offsets, fixed time zone rules, or rely on tables of time zone data. I usually advise against any of these approaches because they are too brittle.

  • Fixed offsets are bad because they don't account for daylight saving time.
  • Fixed rules are bad because time zone rules can (and do) change. Editing stored procs to keep up with these changes is too fragile (IMHO).
  • Maintaining tables of time zone data is a little better, but usually I find these tables to not be maintained well. If you go down this route, be sure to put a procedure in place for updating the tables periodically.
Share:
10,269
Dennis
Author by

Dennis

Updated on June 04, 2022

Comments

  • Dennis
    Dennis almost 2 years

    I have a Classic ASP application that I am working with date cut offs. My server resides in Central Time, but I am in Eastern time. What happens is my app thinks it is an hour earlier and my cut offs are an hour late. I am sure they would be 2 hours early if a user was in Pacific time.

    What I am trying to figure out is if there is a way to either

    1. tell the server to show me local time when you do a GetDate() on SQL or Now() in ASP
    2. figure out some way to do an offset that I can run when the page first loads and use as needed.

    I tried server side javascript, it returns Central Time too. Any help would be greatly appreciated!! Thanks in advance!

    Dennis


    UPDATE - 4/11/12 @ 1:12pm:


    I think that I found a work around for my application, but it would not work generically. I have geographic data for the location I am working with - zip code. I can grab the timezone from that - it would not fully work right for users in other timezones looking at the location, but it does not matter for my app since I just need to be focused on the end time for that location.

    This is the other other way(s) I found were provided by JohnB below (specifically #4). thanks everyone. http://www.webmasterworld.com/forum47/600.htm (bottom)


  • Dennis
    Dennis about 12 years
    Thanks for the reply! I am going to fully go over the links etc in a bit, but I have one more question. What if there is no user input and you cannot get the user to select the date time beforehand? There needs to be a way of knowing the timezone by default. If not, there is really no way to do expire times. This is very interesting!!
  • Dennis
    Dennis about 12 years
    Hi there. I just tried this. Actually, it did not work. I dropped the code from the article on my server and all the times say exactly the same. It might be that I do not have the other display languages installed, not sure. I do not need other countries though, just time zones for the USA. Interesting stuff though, just got a bit of a lesson! :)
  • Dennis
    Dennis about 12 years
    This is very close to what I was using before, but the issue is that it assumes you know the current time zone of the user...
  • msigman
    msigman about 12 years
    To get the time zone of the user I'd just use a JavaScript/jQuery function to send it to the server. I'll update my answer...
  • JohnB
    JohnB about 12 years
    1. You can get time automatically from their client (i.e. laptop/phone) and compare to server time to calculate offset. However (see #3), the user is constantly moving and possibly changing time zones. Furthermore, you are relying them having their clock set correctly, which is a bad assumption. If you still wish to go that route, see my edit above. 2. I'm not sure exactly what you mean by expire times, so we might be able to provide better help if you provide more info.
  • JohnB
    JohnB about 12 years
  • John
    John about 9 years
    That just determines how the date and time are formatted, it doesn't change the actual time. (Eg in the UK we format dates dd/mm/yy, but in the US they use mm/dd/yy)
  • Matt Johnson-Pint
    Matt Johnson-Pint about 9 years
    jstz is good to find the time zone id, but one needs to be very careful in how it is applied on the server side. If you just apply the numeric offset, you won't be accounting for daylight saving time properly. See "Time Zone != Offset" in the timezone tag wiki.
  • WilliamK
    WilliamK almost 2 years
    Question was about "Classic ASP"and not .Net.
  • Matt Johnson-Pint
    Matt Johnson-Pint almost 2 years
    @WilliamK - Yes, and the answer was also about Classic ASP.