Convert UTC to Date

L

Lonifasiko

Hi,

I want to convert an UTC time to a Date object in Javascript.

The UTC time we have is a string that looks like "1160720058.377452373"
for example.

I've done it in Java but I'm not able to do it via Javascript, that
should be pretty similar...

I would really appreciate any help on this.

Thansk very much in advance.
 
P

Pi

I found this code in an old project I did, hope it helps. You might
have to mess with it little first.

function timeToHuman(x){
var theDate = new Date();
theDate.setTime(parseInt(x)); //this would remove the
millisecond part of it.

var dateString = theDate.toGMTString(); //guessing
you'll now have the date in string format here. To get individual
elements...

var arrDateStr = dateString.split(" ");
var month = getMonthNum(arrDateStr[2]);
var day = arrDateStr[1];
var year = arrDateStr[3];
var hour = arrDateStr[4].substr(0,2);
var minute = arrDateStr[4].substr(3,2);
var second = arrDateStr[4].substr(6,2);
if (x==0) return ("");
else return (day+ '/' + month + '/' + year + ' ' + hour +
':' + minute + ':' + second)

}

function getMonthNum(abbMonth){
var arrMon = new
Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var i;
for(i=0; i<arrMon.length; i++)
{
if(abbMonth == arrMon)
return i+1;
}

return -1;
}
 
E

Evertjan.

Lonifasiko wrote on 10 jan 2007 in comp.lang.javascript:
Hi,

I want to convert an UTC time to a Date object in Javascript.

The UTC time we have is a string that looks like "1160720058.377452373"
for example.

You did not define the unit you are using,
but if it is minutes since 19700101, try:

r = 1160720058.377452373
d = new Date(r*1000)
alert(r);
I've done it in Java but I'm not able to do it via Javascript, that
should be pretty similar...

Java has nothing to do with javascript, but the name.

Javascript counts time in miliseconds.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Lonifasiko wrote on 10 jan 2007 in comp.lang.javascript:

You should have given what Gregorian date that represents, preferably in
ISO 8601 form.

You did not define the unit you are using,
but if it is minutes since 19700101, try:

seconds since 1970-01-01 00:00:00 GMT or UTC.
r = 1160720058.377452373
d = new Date(r*1000)
alert(r);

That will truncate, rather than round, to integer milliseconds. For
rounding, add 5e-4 - and if any dates may be before epoch, check what
happens there.

Before the multiplication, javascript should round the string to an IEEE
Double.

MS = 1000 * "1160720058.377452373"
Do = new Date(MS)

The Object Do represents 2006-10-13 06:14:18 UTC

It's a good idea to read the newsgroup and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top