getting GMT in a particular format

W

wolverine

Hi,
I want to get gmt in the format "MM/DD/YY HR:MIN:SEC". I have
written the following code. Could any one of you suggest a better
way ?

var localTime = new Date();
var utc = localTime.getTime() +
(localTime.getTimezoneOffset() * 60000 );
var currTime = new Date(utc); //gmt time

var month = AppendLeadingZero(currTime.getMonth() + 1);
var date = AppendLeadingZero(currTime.getDate());
var hours = AppendLeadingZero(currTime.getHours());
var year = currTime.getFullYear().toString().substr(2,2);
var minutes = AppendLeadingZero(currTime.getMinutes());
var seconds = AppendLeadingZero(currTime.getSeconds());
var gmt = month + "/" + date + "/" + year + " " + hours + ":"
+ minutes + ":" + seconds;

Is there any better way in javascript to get gmt in this format ?

Thanks In Advance
Kiran.
 
E

Evertjan.

wolverine wrote on 05 sep 2007 in comp.lang.javascript:

I want to get gmt in the format "MM/DD/YY HR:MIN:SEC".

Are you "into" ancient defective forms of date notation?
I have
written the following code. Could any one of you suggest a better
way ?

var localTime = new Date();
var utc = localTime.getTime() +
(localTime.getTimezoneOffset() * 60000 );
var currTime = new Date(utc); //gmt time

var month = AppendLeadingZero(currTime.getMonth() + 1);
var date = AppendLeadingZero(currTime.getDate());
var hours = AppendLeadingZero(currTime.getHours());
var year = currTime.getFullYear().toString().substr(2,2);
var minutes = AppendLeadingZero(currTime.getMinutes());
var seconds = AppendLeadingZero(currTime.getSeconds());

This gives you the current UTC time in a string:

function t(x){return ((x>9)?'':'0')+x};

d = new Date();
var UTCtime =
t(d.getUTCHours()) + ':' +
t(d.getUTCMinutes()) + ':' +
t(d.getUTCSeconds());
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
legroups.com>, Wed, 5 Sep 2007 04:36:26, wolverine
I want to get gmt in the format "MM/DD/YY HR:MIN:SEC". I have
written the following code. Could any one of you suggest a better
way ?

That's a silly way to write a date. The International Standard, and
common sense, calls for YYYY-MM-DD HH:MM:SS. But for the USA, surely
you need the 12-hour clock?
var localTime = new Date();
var utc = localTime.getTime() +
(localTime.getTimezoneOffset() * 60000 );
var currTime = new Date(utc); //gmt time

var month = AppendLeadingZero(currTime.getMonth() + 1);
var date = AppendLeadingZero(currTime.getDate());
var hours = AppendLeadingZero(currTime.getHours());
var year = currTime.getFullYear().toString().substr(2,2);
var minutes = AppendLeadingZero(currTime.getMinutes());
var seconds = AppendLeadingZero(currTime.getSeconds());
var gmt = month + "/" + date + "/" + year + " " + hours + ":"
+ minutes + ":" + seconds;

Is there any better way in javascript to get gmt in this format ?

For the present purpose, GMT is now called UTC. To build a UTC
date/time, use the UTC methods.

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

wolverine

wolverine wrote on 05 sep 2007 in comp.lang.javascript:


Are you "into" ancient defective forms of date notation?




This gives you the current UTC time in a string:

function t(x){return ((x>9)?'':'0')+x};

d = new Date();
var UTCtime =
t(d.getUTCHours()) + ':' +
t(d.getUTCMinutes()) + ':' +
t(d.getUTCSeconds());

Thanks for the answer. But i expected single function to give a UTC
time in any particular format (say YYYY-MM-DD HH:MM:SS).

Once again THANKS a LOT.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
This gives you the current UTC time in a string:

It seems likely that new Date().toUTCString().match(/\S{8}/) will
always give that (in an Object); but it's not guaranteed by spec.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top