PDA

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



leonidassavvides
05-24-2008, 09:26 AM
If I use:

<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 ?

jscheuer1
05-24-2008, 02:46 PM
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:


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:


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.

leonidassavvides
05-24-2008, 04:56 PM
this is correct for a visitor to my site anywhere is, get gmt+2 time ?


<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>

jscheuer1
05-25-2008, 04:38 AM
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:


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.

leonidassavvides
05-25-2008, 01:28 PM
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()) ) ?

leonidassavvides
05-25-2008, 02:31 PM
This is correct ?


<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 ?

jscheuer1
05-25-2008, 04:24 PM
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:


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):


<!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>

jscheuer1
05-29-2008, 03:32 PM
This is about as good as it gets using javascript alone:


<!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.

jscheuer1
05-31-2008, 05:35 PM
This also looks promising (see also my previous post):

http://www.timeanddate.com/worldclock/aboutpersonal.html