Results 1 to 9 of 9

Thread: how to convert gmt date(given by d.toUTCString();) to milliseconds from utc 1970

  1. #1
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default how to convert gmt date(given by d.toUTCString();) to milliseconds from utc 1970

    If I use:
    Code:
    <script type="text/javascript">
    
    var d = new Date();
    gmt=d.toUTCString();
    document.write (d.toUTCString());
    </script>
    then how to convert gmt date to milliseconds from utc 1970, and then add for my zone (gmt+2) 60*60*1000*2 and then convert totaled milliseconds in date like gmt=d.toUTCString();
    ???
    My zone is gmt+2 but gmt time is lag 3hours, why this and not lag 2hours as normal ?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The additional lag is probably due to Daylight Savings Time (that, or an inaccurate clock on the computer). You can get the time offset from Greenwich using:

    Code:
    d.getTimezoneOffset()
    Which returns that value in minutes. But, if you just want the milliseconds from 1970, why convert to UTC in the first place?

    To get the milliseconds from 1970, just do:

    Code:
    document.write (Date.parse(new Date()));
    See also:

    http://msdn.microsoft.com/en-us/library/cd9w2te4.aspx

    particularly at the bottom where it links to all of the various date methods.
    Last edited by jscheuer1; 05-24-2008 at 03:19 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    this is correct for a visitor to my site anywhere is, get gmt+2 time ?

    Code:
    <script type="text/javascript">
    
    var d = new Date();
    // gmt=d.toUTCString();
    // document.write (d.toUTCString());
    var d_gmt2_millisec = new Date(valueOf(d.toUTCString())+60*60*1000*2);
    document.write(d_gmt2_millisec.toString());
    </script>

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You perhaps need to do a bit of trial and error testing to see if what you want is getting generated by your code, as It isn't really clear to me what you are after.

    However, if you just want the time in milliseconds from 0hr 1/1/1970:

    Code:
    Date.parse(new Date())
    holds/expresses that value. It doesn't need to be converted from local time to UTC and back to local time, as the values added and subtracted, or visa versa, will add up to the same thing and be cancelled out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    I want the time in milliseconds from 0hr 1/1/1970 but in GMT+2, to put in my site ... this I do NOT want to change if a visitor comes from eg. USA ...
    When I have the time in milliseconds from 0hr 1/1/1970 but in GMT+2, what to use to make it STRING ? eg Sun, 25 May 2008 13:26:49 ...?

    the above you say what is var Date ( in Date.parse(new Date()) ) ?

  6. #6
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    This is correct ?
    Code:
    <script type="text/javascript">
    
    var d = new Date();
    gmt=d.toUTCString();    // get  GMT Time/date 
    // document.write (d.toUTCString());
    var gmt2ms = new Date(gmt.valueOf()+60*60*1000*2); // add two hours in ms
    document.write(gmt2ms.toString());  //  write date as Sun, 25 May 2008 14:19:30 
    </script>
    What the
    x.valueOf()
    x.toString()
    do ?

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by leonidassavvides View Post
    what is var Date ( in Date.parse(new Date()) ) ?
    It's not a variable, it is the javascript Date object. Not any particular Date object, the built in Date object of javascript. It's parse() method (when applied to a specific Date object returns the milliseconds from 0hr 1/1/1970.

    Now, if you want the difference in milliseconds between now and 0hr 1/1/1970, it doesn't matter what time zone you are in. As long as the computer's clock is set properly for that time zone:

    Code:
    Date.parse(new Date())
    will hold the correct value when executed anywhere in the world.

    But I'm still not sure what you are after, if you want the current time as it would be displayed on your computer regardless of where the computer doing this is, you would want to do this (there could be other ways):

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <script type="text/javascript">
    var d = new Date();
    d.setMinutes(d.getMinutes()+d.getTimezoneOffset()+2*60);
    var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
    months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    dd = days[d.getDay()],
    mn = months[d.getMonth()],
    dt = d.getDate(),
    yy = d.getFullYear(),
    hh = d.getHours(),
    mm = d.getMinutes(),
    ss = d.getSeconds();
    
    document.write (dd+' '+mn+' '+dt+' '+yy+' '+hh+':'+mm+':'+ss+' GMT+0200');
    </script>
    </body>
    </html>
    Last edited by jscheuer1; 05-26-2008 at 07:06 AM. Reason: correct the math
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This is about as good as it gets using javascript alone:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    #infocypruseutime {
    text-align:center;
    font-size:90%;
    font-family:sans-serif;
    }
    </style>
    <script type="text/javascript">
    
    var findCyprusEuTimeDST = function(year, mo, dt) {
    
    /***********************
      DST in Cyprus-EU Zone
    ***********************/
    
        /* set defaults if necessary */
        dt = dt | 1; mo = mo || 2; year = year | new Date().getUTCFullYear();
       /* declare our variables - using 12 noon should avoid any unpleasantness from DST */
       var oDate = new Date(year,mo,dt); oDate.setHours(12); var day = oDate.getDay(), mm, dd = oDate.getDate() - day, yy,
       /* day will be how far we are from the previous Sunday, subtracting it from the date and will give us Sunday */
    
        dd = dd + 7; /* advance to the next Sunday */
    
    oDate.setDate(dd); /* pass our new date back to the oDate Date object for any change in month figure */
    
        mm = oDate.getMonth();
        dd = oDate.getDate();
        yy = oDate.getFullYear();
        if (mm == mo){ // if we haven't advanced the month yet we either have the Sunday we are looking for, or are still checking
        findCyprusEuTimeDST.dat[(mo == 2? 0 : 1)] = Date.parse(new Date(oDate.setUTCHours(1))); // using UTC 01:00:00 should place us at the DST transition moment in zone +0200
        findCyprusEuTimeDST(yy,mm,dd);
        }
        else if (mm - 1 == 2) findCyprusEuTimeDST('', 9);
        else if (mm - 1 == 9) findCyprusEuTimeDST.dat[2] = 'Done!';
    }
    
    findCyprusEuTimeDST.dat = [];
    findCyprusEuTimeDST();
    
    function cyprusEuTime(){
    
    	if(findCyprusEuTimeDST.dat.length != 3){
    		setTimeout('cyprusEuTime()', 300);
    		return;
    	}
    
    var    d = new Date(),
          dp = Date.parse(d),
         dst = dp >= findCyprusEuTimeDST.dat[0] && dp < findCyprusEuTimeDST.dat[1]? 1 : 0;
    
    d.setMinutes(d.getMinutes()+d.getTimezoneOffset()+(2+dst)*60);
    
    var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
      months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
         sfx = {1:'st', 2:'nd', 3:'rd', 21:'st', 22:'nd', 23:'rd', 31:'st'},
         pad = function(v){return v.toString().length == 2? v : '0' + v;},
          dd = days[d.getDay()],
          mn = months[d.getMonth()],
          dt = d.getDate(),
          yy = d.getFullYear(),
          hh = pad(d.getHours()),
          mm = pad(d.getMinutes()),
          ss = pad(d.getSeconds());
         sfx = sfx[dt]? sfx[dt] : 'th';
    
    cyprusEuTime.dat = dd+' '+mn+' '+dt+sfx+' '+yy+' '+hh+':'+mm+':'+ss+' GMT+0'+(2+dst)+'00\x0d\x0a'+
    	(dst? 'Eastern European Summer ' : 'Eastern European Standard ')+'Time Cyprus-EU';
    
    document.getElementById('infocypruseutime').firstChild.nodeValue = cyprusEuTime.dat;
    
    setTimeout(function(){findCyprusEuTimeDST.dat = []; findCyprusEuTimeDST(); cyprusEuTime();}, 1000);
    
    }
    
    window.onload = cyprusEuTime;
    </script>
    </head>
    <body>
    <pre id="infocypruseutime">&nbsp;</pre>
    </body>
    </html>
    The code could probably be simplified, and there could be a bug, if say the user's computer changes to DST at an inopportune moment, but I think I got away from that by using noon where noted in the code. It will depend upon the user's clock being accurate. It also depends upon Cyprus continuing to use the same rules for changing to and from Summer Time. Here is an alternative - a link to a free Flash clock from worldtimeserver.com:

    http://www.worldtimeserver.com/clocks/

    The clock may be customized as to zone and somewhat also as to its appearance.
    Last edited by jscheuer1; 05-30-2008 at 09:50 AM. Reason: add periodic year update for DST function
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This also looks promising (see also my previous post):

    http://www.timeanddate.com/worldcloc...tpersonal.html
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •