By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,279 Members | 2,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,279 IT Pros & Developers. It's quick & easy.

Convert UTC to Date

P: n/a
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.

Jan 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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","A ug","Sep","Oct","Nov","Dec");
var i;
for(i=0; i<arrMon.length; i++)
{
if(abbMonth == arrMon[i])
return i+1;
}

return -1;
}
Lonifasiko wrote:
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.
Jan 10 '07 #2

P: n/a
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 10 '07 #3

P: n/a
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Wed, 10 Jan 2007 09:03:25, Evertjan. <ex**************@interxnl.net>
posted:
>Lonifasiko wrote on 10 jan 2007 in comp.lang.javascript:
>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 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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.