#1
  1. No Profile Picture
    ASP GOD
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2003
    Location
    Jordan
    Posts
    83
    Rep Power
    16

    Question get user date and time


    Hello all

    I have a web-based program that acts as a (time-punch) machine for employees at a store.
    I faced a problem when publishing the web-based program on the internet for the employees to start using, the problem that there are two stores on two different time zones, and whenever an employee (clocked-in) or (clocked-out) the time is taken using time() and date() functions, as we all know, those are server side functions, so they fetch the time and date of the SERVER, and my problem comes here.
    This also means that if i ever change the webhosting company, the other copmany might have their servers on another time-zone, so it will be difficult to know the real time.

    Bottom line is that i want to know if there is a possible way to read the date and time from the user machine that he or she is using to clock-in or clock-out.

    Thanks in advance.
    There is always time to play Descent
  2. #2
  3. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Apr 2004
    Location
    Australia
    Posts
    9
    Rep Power
    0
    Hi,

    You can use client side javascripting to read the date and time from the local machine, and submit it along with the rest of the the form.
  4. #3
  5. No Profile Picture
    ASP GOD
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2003
    Location
    Jordan
    Posts
    83
    Rep Power
    16

    and the javascripts are......


    thanks for the note, it could've been more helpful if you added the names of the javascripts because i'm beginner with javascripts. However, i'll look for them, but just incase, i would appreciate if you recommended the script or scripts to use.

    Thanks again.

    D.
  6. #4
  7. No Profile Picture
    Grumpier Old Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2003
    Posts
    10,143
    Rep Power
    191
    You may want to think twice about this approach, unless you are sure the client computers have their clocks properly set. I would look at storing a time zone offset in each user's record and use the server's clock.
    ======
    Doug G
    ======
    I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain
  8. #5
  9. No Profile Picture
    ASP GOD
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2003
    Location
    Jordan
    Posts
    83
    Rep Power
    16
    i thought of that, but this would be risky as web-hosting companies may change servers any moment. Plus, the computers my employees will use to login are locked and controlled by management only so it's not chance to get played with.
    if i had my own server, that would be the optimal solution, but considering i'm relying on webhosting companies, i still (think) the optimal solution is to fetch the time from the user machine.

    What do you (anyone) think?
  10. #6
  11. Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Mar 2004
    Location
    Florida
    Posts
    587
    Rep Power
    16
    The javascript you asked for:

    Place this function in the <head> section

    Code:
    function getTime() {
    var date = new Date();
    return date;
    }
    Place this code in your form (I am assuming a form named "form1")
    Code:
    <input type="hidden" name="clienttime">
    <script language="JavaScript">
    document.form1.clienttime.value = getTime();
    </script>
    You can now use request.form("clienttime") to pull the clients date/time value from the form.
    Neal Schafer
    The early worm gets eaten.
  12. #7
  13. No Profile Picture
    ASP GOD
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2003
    Location
    Jordan
    Posts
    83
    Rep Power
    16
    thanks for that really appreciate it.

    So i assume if i used:

    document.form1.clienttime.value = getDate();

    it would return the date value only (i.e. 4/25/2004 ) only ?

    I have another question. does the format of the date (mm/dd/yyyy) or (dd/mm/yyyy) will be received (as is) from the user machine or the server will automatically convert it to its own format no matter what was the client machine date format?
  14. #8
  15. Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Mar 2004
    Location
    Florida
    Posts
    587
    Rep Power
    16

    Lightbulb Getting Time Zone Information from Server


    If you are using a SQL 2000 server for storing your data the following will work. This example actually just allows you to find the time in any tme zone. You could do this with javascript and the client's time, but I'm trying to point out that this will allow you to base time on the server's time rather than the client. Even if your provider moves their servers this should work.

    SQL 2000 provides a function getUTCDate() that provides Universal Time. This means that the time stored can always be based on Universal Time rather than on a specific time zone. All you need to do to determine actual time is to place a TimeZone offset in your database for each location. This offset is not based on server time, but on GMT so it doesn't matter if the server moves.

    The other key here is the use of Daylight Savings Time. You need to know if you are within DST date ranges, and if the location clocking in uses DST. The code below will determine if you are within the date range (I can't remember exactly where I pulled this from) and I would store the GMT offset for each location in my location database.

    I hope this code helps you. It's not completely on target but everything you need is here and it's just a matter of pulling the appropriate data.

    NOTE: To the best of my knowledge this only works with MS SQL 2000 server. If you are using Access or an earlier version of SQL it probably won't work. I don't know about MySQL.

    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Time Zone Display</title>
    <meta name="Microsoft Theme" content="sumipntg 1011, default">
    <Script Language="JavaScript">
    // ------------------------------------------------------------------------
    // The following Functions will determine if we are currently in a date
    // that falls within Daylight Savings Time Range
    function makeArray()	{
    	this[0] = makeArray.arguments.length;
    	for (i = 0; i<makeArray.arguments.length; i++)
    		this[i+1] = makeArray.arguments[i];
    }
    var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    function LeapYear(year) {
    	if ((year/4)   != Math.floor(year/4))   return false;
    	if ((year/100) != Math.floor(year/100)) return true;
    	if ((year/400) != Math.floor(year/400)) return false;
    	return true;
    }
    function NthDay(nth,weekday,month,year) {
    	if (nth > 0) return (nth-1)*7 + 1 + (7 + weekday - DayOfWeek((nth-1)*7 + 1,month,year))%7;
    	if (LeapYear(year)) var days = daysofmonthLY[month];
    	else				var days = daysofmonth[month];
    	return days - (DayOfWeek(days,month,year) - weekday + 7)%7;
    }
    function DayOfWeek(day,month,year) {
    	var a = Math.floor((14 - month)/12);
    	var y = year - a;
    	var m = month + 12*a - 2;
    	var d = (day + y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    	return d+1;
    }
    function y2k(number)	{ return (number < 1000) ? number + 1900 : number; }
    function getMS(date) {
    	return Date.UTC(y2k(date.getYear()),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds())
    }
    function isDST() {
    var today = new Date();
    var year = y2k(today.getYear());
    var DSTstart = new Date(year,4-1,NthDay(1,1,4,year),2,0,0);
    var DSTend   = new Date(year,10-1,NthDay(-1,1,10,year),2,0,0);
    var todayMS = getMS(today);
    var DSTstartMS = getMS(DSTstart);
    var DSTendMS = getMS(DSTend);
    if (todayMS > DSTstartMS && todayMS < DSTendMS)
    	return 1;
    else
    	return 0;
    }
    // End Daylight Savmings Time Functions
    /// ---------------------------------------------------------
    function GetTime() { 
    var dt = new Date(document.form1.utctime.value);   // Get SQL server UTC time value
    var gmt = (dt.getHours());			   // Determine the Hours portion of the UTC Time
    var tz = parseInt(document.form1.TimeZone.value);		// Get The Time Zone Desired from the Pull Down
    if (document.form1.usedst.checked) tz = tz + isDST();  // If we have elected to use Daylight Savings Time check if we need to add an hour
    if (tz < 0) 			   
     {
     if (gmt + tz < 0) 
    	var hours = 24 - gmt + tz ;
       else
    	var hours = gmt + tz;
     }
    else
     {
     if (gmt + tz >= 24) 
    	var hours = gmt + tz - 24;
       else
    	var hours = gmt + tz;
     } 
    return((IfZero(hours) + ":" + IfZero(dt.getMinutes()) + ":" + IfZero(dt.getSeconds())));  // return the time for the specified time zone
    }						// the IfZero function pads numbers less than 10 with a 0. 
    function IfZero(num) {
    return ((num <= 9) ? ("0" + num) : num);
    }
    </script>
    </head>
    <body>
    <!-- #include file="dataopen.asp" -->
    <% set rs = conn.execute("Select getUTCDate() as utcdate")%>
    <form name="form1" method="post">
    <input type="hidden" name="utctime" value="<%=rs("utcDate")%>">
    <input type="hidden" name="dst">
    <script language = "JavaScript">
     document.form1.dst.value = isDST();
    </script>
    <input type="checkbox" name="usedst" checked>Use Daylight Savings Time
    <BR>
    <select name="TimeZone" onchange="document.form1.clienttime.value=GetTime();">
      <option value=-12> Eniwetok (GMT - 12)</option>
      <option value=-11> Somoa (GMT - 11)</option>
      <option value=-10> Hawaii (GMT - 10)</option>
      <option value=-9> Alaska (GMT - 9)</option>
      <option value=-8> Pacific Time (GMT - 8)</option>
      <option value=-7> Mountain Time (GMT - 7)</option>
      <option value=-6> Central Time (GMT - 6)</option>
      <option value=-5> Eastern Time (GMT - 5)</option>
      <option value=-4> Atlantic Time (GMT - 4)</option>
      <option value=-3> Brazilia (GMT - 3)</option>
      <option value=-2> Mid-Atlantic (GMT - 2)</option>
      <option value=-1> Azores (GMT - 1)</option>
      <option value=0 selected> GMT </option>
      <option value=1> Rome (GMT + 1)</option>
      <option value=2> Israel (GMT + 2)</option>
      <option value=3> Moscow (GMT + 3)</option>
      <option value=4> Baku (GMT + 4)</option>
      <option value=5> New Delhi (GMT + 5)</option>
      <option value=6> Dhakar (GMT + 6)</option>
      <option value=7> Bangkok (GMT + 7)</option>
      <option value=8> Hong Kong (GMT + 8)</option>
      <option value=9> Tokyo (GMT + 9)</option>
      <option value=10> Sydney (GMT +10)</option>
      <option value=11> Magadan (GMT +11)</option>
      <option value=12> Wellington (GMT +12)</option>
    </select>
    <input type="text" name="clienttime">
    </body>
    </html>
  16. #9
  17. No Profile Picture
    ASP GOD
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2003
    Location
    Jordan
    Posts
    83
    Rep Power
    16
    Thanks NS, and yes i'm using Access 2000, but i (think) i will be able to suit it with the code you've given. i'm sure i will be able to pull out of it what i need. However, it surly seems a lot of work but at the end when it's done it will relief me from lot of weekly paper work

    Thanks a lot. I will post back if i encountered something else.
  18. #10
  19. Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Mar 2004
    Location
    Florida
    Posts
    587
    Rep Power
    16
    Sorry it took me so long to get back, and I see that it looks like you have thing in hand, but since I realized an earlier post I did here was incomplete, I thougt I'd go ahead and improve on the javascript I posted earlier.

    this will post the data in MM/DD/YYYY HH:MM:SS format (24 hour format). I'm sure you can see how your could change this if you desired.

    I hope this helps.

    Code:
    <script Language = "JavaScript">
    function getDate() {
    var date = new Date();
    var strDate = IfZero(date.getMonth()) + "/" + IfZero(date.getDate()) + "/" + date.getFullYear()+ " " ;
    strDate += IfZero(date.getHours()) + ":" + IfZero(date.getMinutes()) + ":" + IfZero(date.getSeconds());
    return strDate
    }
    function IfZero(num) {
    return ((num <= 9) ? ("0" + num) : num);
    }
    </script>

Similar Threads

  1. ASP displaying a date instead of a time?
    By K0r34nB0y in forum ASP Development
    Replies: 2
    Last Post: February 24th, 2004, 11:42 PM
  2. Time tracking / Date time function with timezones
    By Ralph in forum ASP Development
    Replies: 2
    Last Post: January 5th, 2004, 11:54 PM
  3. Date and time
    By thehen in forum Programming Help
    Replies: 0
    Last Post: October 16th, 2003, 02:17 AM
  4. count no of time submitted and date issue
    By jaishankar in forum ASP Development
    Replies: 2
    Last Post: October 10th, 2003, 06:39 AM
  5. controlling date and time formats in an asp-generated email
    By Steve Schofield in forum ASP Development
    Replies: 1
    Last Post: January 22nd, 2001, 01:04 PM

IMN logo majestic logo threadwatch logo seochat tools logo