468,535 Members | 1,629 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,535 developers. It's quick & easy.

UTC date and time to local

Hi,

I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezoneOffset();
if(offSet < 0) {
inDateMod.setMinutes(inDateMod.getMinutes()+offSet );
} else {
inDateMod.setMinutes(inDateMod.getMinutes()-offSet);
}
return inDateMod;
}

Can anyone help with this? Or does anyone have a code that would do
this for me?
Thanks
Maz.

Apr 9 '06 #1
13 15632
<ma*******@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Hi,

I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezoneOffset();
if(offSet < 0) {
inDateMod.setMinutes(inDateMod.getMinutes()+offSet );
} else {
inDateMod.setMinutes(inDateMod.getMinutes()-offSet);
}
return inDateMod;
}

Can anyone help with this? Or does anyone have a code that would do
this for me?
Thanks
Maz.


Take a look at the
dateObj.setUTCHours(hh,mm) method -
If you set those, then the (local) time will be set automagically.
At least it does in Firefox, where I just tested it.
http://developer.mozilla.org/en/docs...s:Date#Methods
Hope this helps
Apr 9 '06 #2
ma*******@gmail.com wrote:
I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezoneOffset();
if(offSet < 0) {
inDateMod.setMinutes(inDateMod.getMinutes()+offSet );
} else {
inDateMod.setMinutes(inDateMod.getMinutes()-offSet);


Date.prototype.getMinutes() already returns the minutes in the
local time.

If you really wanted to convert from UTC to local time (which is
entirely redundant), you would need to retrieve the value with
Date.prototype.getUTCMinutes() instead.
PointedEars
Apr 9 '06 #3
JRS: In article <11**********************@v46g2000cwv.googlegroups .com>
, dated Sat, 8 Apr 2006 22:00:21 remote, seen in
news:comp.lang.javascript, ma*******@gmail.com posted :
I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
Inefficient, if inDate arrives as a Date Object : use
var inDateMod = new Date(+inDate);
to copy the value of a Date Object.
offSet = today.getTimezoneOffset();
if(offSet < 0) {
inDateMod.setMinutes(inDateMod.getMinutes()+offSet );
} else {
inDateMod.setMinutes(inDateMod.getMinutes()-offSet);
}
return inDateMod;
}

Can anyone help with this? Or does anyone have a code that would do
this for me?


Anyone who thinks it appropriate to add negative offsets and subtract
positive ones is not worth copying from, and should be advised to take
up knitting instead of computing.

Including the current offset in a non-current time seems unlikely to be
useful.

Be aware that crossing a Summer Time change with setMinutes() may not
give the expected result.

The only reason I can at present see for doing something like that would
be when dealing with someone else's local time. But as it does not do
what you claim, and neither makes sense, it's hard to see what you do
need.

inDateMod = new Date(+inDate +- 6e4*new Date().getTimezoneOffset())

should do much what your code's programmer was thinking of, but more
efficiently.

Read the newsgroup FAQ; see below.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 9 '06 #4

"Hal Rosser" <hm******@bellsouth.net> wrote in message
news:jO*****************@bignews7.bellsouth.net...
<ma*******@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Hi,

I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

Take a look at the
dateObj.setUTCHours(hh,mm) method -
If you set those, then the (local) time will be set automagically.
At least it does in Firefox, where I just tested it.

http://developer.mozilla.org/en/docs...s:Date#Methods Hope this helps


Try this -( assumes the time zone is set correctly in the machine using the
code)
// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var inDateMod = new Date(inDate);
inDateMod.setUTCHours( inDate.getHours()) ; //**
return inDateMod;
}// ** (the minutes and seconds were set when created from inDate)

Apr 9 '06 #5
Dr John Stockton wrote on 09 apr 2006 in comp.lang.javascript:
inDateMod = new Date(+inDate +- 6e4*new Date().getTimezoneOffset())

should do much what your code's programmer was thinking of, but more
efficiently.


Why the +- operator?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 10 '06 #6
JRS: In article <Xn********************@194.109.133.242>, dated Mon, 10
Apr 2006 08:27:23 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
Dr John Stockton wrote on 09 apr 2006 in comp.lang.javascript:
inDateMod = new Date(+inDate +- 6e4*new Date().getTimezoneOffset())

should do much what your code's programmer was thinking of, but more
efficiently.


Why the +- operator?


Because the character given by ± in HTML is unsuitable for plain-
text News; as the original requirement does not make much direct sense,
I didn't trouble to decide whether + or - might be better.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Apr 10 '06 #7
JRS: In article <nN*****************@bignews5.bellsouth.net>, dated
Sun, 9 Apr 2006 19:32:44 remote, seen in news:comp.lang.javascript, Hal
Rosser <hm******@bellsouth.net> posted :
<ma*******@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
> I have the following function to convert UTC time to Local time. It
> works perfect for GMT- (Minus) time zones however it provides incorrect
> results for GMT+(Plus) time zones?
Try this -( assumes the time zone is set correctly in the machine using the
code)
// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var inDateMod = new Date(inDate);
inDateMod.setUTCHours( inDate.getHours()) ; //**
return inDateMod;
}// ** (the minutes and seconds were set when created from inDate)


You are four hours behind Greenwich Mean Time at present, it seems.

If that code is executed at 7.30 p.m. local = 23:30 GMT, the fundamental
contents of inDateMod will be reduced by four hours.

If that code is executed at 8.30 p.m. local = 00:30 GMT, the fundamental
contents of inDateMod will be increased by twenty hours.

One of those might be useful; it seems unlikely that both will be.

Now consider your readers in Mumbai, if any there be. Their local time
is 5h 30m ahead of GMT, so there seems to be an additional effect
changing each half-hour when using your code. The same applies, for
half the year, to readers in Lord Howe Island.

It's obvious enough, as previously indicated, why the original code
gives a difference between localities on each side of GMT; but as what
the OP really needs is a mystery, one can offer little more.

The OP should read the newsgroup FAQ; see below.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 10 '06 #8

"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:Rl**************@merlyn.demon.co.uk...
JRS: In article <nN*****************@bignews5.bellsouth.net>, dated
Sun, 9 Apr 2006 19:32:44 remote, seen in news:comp.lang.javascript, Hal
Rosser <hm******@bellsouth.net> posted :
<ma*******@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...

Now consider your readers in Mumbai, if any there be. Their local time
is 5h 30m ahead of GMT, so there seems to be an additional effect
changing each half-hour when using your code. The same applies, for
half the year, to readers in Lord Howe Island.

It's obvious enough, as previously indicated, why the original code
gives a difference between localities on each side of GMT; but as what
the OP really needs is a mystery, one can offer little more.

The OP should read the newsgroup FAQ; see below.


I hope the OP was not in Kathmandu, Kabul, or Rangoon or Darwin.
some with x-and-a-half or x-and-3-quarter hours difference from zulu time.
Apr 11 '06 #9

"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:Rl**************@merlyn.demon.co.uk...
JRS: In article <nN*****************@bignews5.bellsouth.net>, dated
Sun, 9 Apr 2006 19:32:44 remote, seen in news:comp.lang.javascript, Hal
Rosser <hm******@bellsouth.net> posted :
<ma*******@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com... I have the following function to convert UTC time to Local time. It
> works perfect for GMT- (Minus) time zones however it provides incorrect > results for GMT+(Plus) time zones?
Try this -( assumes the time zone is set correctly in the machine using

thecode)
// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var inDateMod = new Date(inDate);
inDateMod.setUTCHours( inDate.getHours()) ; //**
return inDateMod;
}// ** (the minutes and seconds were set when created from inDate)


You are four hours behind Greenwich Mean Time at present, it seems.

If that code is executed at 7.30 p.m. local = 23:30 GMT, the fundamental
contents of inDateMod will be reduced by four hours.

If that code is executed at 8.30 p.m. local = 00:30 GMT, the fundamental
contents of inDateMod will be increased by twenty hours.

One of those might be useful; it seems unlikely that both will be.

Now consider your readers in Mumbai, if any there be. Their local time
is 5h 30m ahead of GMT, so there seems to be an additional effect
changing each half-hour when using your code. The same applies, for
half the year, to readers in Lord Howe Island.

It's obvious enough, as previously indicated, why the original code
gives a difference between localities on each side of GMT; but as what
the OP really needs is a mystery, one can offer little more.


So - if the user's computer's time and time zone is set correctly,
AND if the user has a "GMT Time" passed to a function, which he want to
convert to his 'Local Time'
THEN all he needs to do is create a date object and use the various
"setUTCxxx" (where xxx is the date, hour, minute) methods. - then the
"toLocaleString" method would show the correct local date and time. Isn't
this correct?
*** but if the user is only concerned with the 'time', then setting the
UTCHour on the date would probably be ok - unless he was in cameroon or
such - or if he was in daylight-savings time, and the date/time he was
converting was referencing a date before the time change. ?? this subject
has worn me out. But I found out some time zones are X plus/minus half-hour
and some three-quarter-hours ...


Apr 12 '06 #10
Based on everyone's input (instead of knitting) I came up with the
following and tested it for + - and GMT time.

function getlocaleDate(inDate)
{
var curUTCDate = new Date(inDate);
// May cause problem in the future b/c of mm/dd/yy format
utcDate = (curUTCDate.getMonth()+1) + "/" + curUTCDate.getDate() +
"/" + curUTCDate.getFullYear();
utcHour = curUTCDate.getHours();
utcMinute = curUTCDate.getMinutes();

var currentDate = new Date(utcDate);

currentDate.setUTCHours(utcHour);
currentDate.setUTCMinutes(utcMinute);
return currentDate;
}

Anyone wants to test it? Might not be very efficient but it seems to be
working.

Maz.

Apr 13 '06 #11

Hal Rosser wrote:
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:Rl**************@merlyn.demon.co.uk...
JRS: In article <nN*****************@bignews5.bellsouth.net>, dated
Sun, 9 Apr 2006 19:32:44 remote, seen in news:comp.lang.javascript, Hal
Rosser <hm******@bellsouth.net> posted :
> <ma*******@gmail.com> wrote in message
> news:11**********************@v46g2000cwv.googlegr oups.com...

> > I have the following function to convert UTC time to Local time. It
> > works perfect for GMT- (Minus) time zones however it provides incorrect> > results for GMT+(Plus) time zones?

Try this -( assumes the time zone is set correctly in the machine using thecode)
// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var inDateMod = new Date(inDate);
inDateMod.setUTCHours( inDate.getHours()) ; //**
return inDateMod;
}// ** (the minutes and seconds were set when created from inDate)


You are four hours behind Greenwich Mean Time at present, it seems.

If that code is executed at 7.30 p.m. local = 23:30 GMT, the fundamental
contents of inDateMod will be reduced by four hours.

If that code is executed at 8.30 p.m. local = 00:30 GMT, the fundamental
contents of inDateMod will be increased by twenty hours.

One of those might be useful; it seems unlikely that both will be.

Now consider your readers in Mumbai, if any there be. Their local time
is 5h 30m ahead of GMT, so there seems to be an additional effect
changing each half-hour when using your code. The same applies, for
half the year, to readers in Lord Howe Island.

It's obvious enough, as previously indicated, why the original code
gives a difference between localities on each side of GMT; but as what
the OP really needs is a mystery, one can offer little more.


So - if the user's computer's time and time zone is set correctly,
AND if the user has a "GMT Time" passed to a function, which he want to
convert to his 'Local Time'
THEN all he needs to do is create a date object and use the various
"setUTCxxx" (where xxx is the date, hour, minute) methods. - then the
"toLocaleString" method would show the correct local date and time. Isn't
this correct?
*** but if the user is only concerned with the 'time', then setting the
UTCHour on the date would probably be ok - unless he was in cameroon or
such - or if he was in daylight-savings time, and the date/time he was
converting was referencing a date before the time change. ?? this subject
has worn me out. But I found out some time zones are X plus/minus half-hour
and some three-quarter-hours ...


Based on everyone's input (instead of knitting) I came up with the
following and tested it for + - and GMT time.

function getlocaleDate(inDate)
{
var curUTCDate = new Date(inDate);
// May cause problem in the future b/c of mm/dd/yy format
utcDate = (curUTCDate.getMonth()+1) + "/" +
curUTCDate.getDate() +
"/" + curUTCDate.getFullYear();
utcHour = curUTCDate.getHours();
utcMinute = curUTCDate.getMinutes();
var currentDate = new Date(utcDate);
currentDate.setUTCHours(utcHour);
currentDate.setUTCMinutes(utcMinute);
return currentDate;
}
Anyone wants to test it? Might not be very efficient but it seems to be

working.
Maz.

Apr 13 '06 #12
JRS: In article <by****************@bignews8.bellsouth.net>, dated Wed,
12 Apr 2006 17:49:11 remote, seen in news:comp.lang.javascript, Hal
Rosser <hm******@bellsouth.net> posted :
So - if the user's computer's time and time zone is set correctly,
Not sufficient. Time Zone is a geographical construct; it changes only
by "political" decision, and not by change of season. It is also
necessary to have the Summer Time Change rules correctly set for the
date/time/location in question.
AND if the user has a "GMT Time" passed to a function, which he want to
convert to his 'Local Time'
THEN all he needs to do is create a date object and use the various
"setUTCxxx" (where xxx is the date, hour, minute) methods. - then the
"toLocaleString" method would show the correct local date and time. Isn't
this correct?
No. Method toLocaleString does not necessarily show the correct local
date and time. It is supposed to, but one cannot rely on the minions of
Mr Gates to get it right. My system, set for UK, outputs 04/13/2006
12:12:27 - but at least it's not the 12-h clock (the systems in the
Public Library appear to be set for US).

For date output to be reliably comprehensible world-wide, use a 4-digit
year and the month in letters (assuming English is understood); or use
YYYY/MM/DD or YYYY-MM-DD with hh:mm:ss, which is understood everywhere,
even in the USA. Generate that by localisation-independent code.

However, what you suggest is an inefficient way of setting a Date Object
to a specified UTC moment; use one of

D = new Date(Date.UTC(Y, M-1, D, h, m, s, ms))
D = new Date("YYYY/MM/DD hh:mm:ss GMT")
D = new Date("YYYY/MM/DD hh:mm:ss Z")

where the first is reliable and the others are probably safe.
*** but if the user is only concerned with the 'time', then setting the
UTCHour on the date would probably be ok - unless he was in cameroon or
Cameroon?
such - or if he was in daylight-savings time, and the date/time he was
converting was referencing a date before the time change. ?? this subject
has worn me out. But I found out some time zones are X plus/minus half-hour
and some three-quarter-hours ...


Indeed : there's a "half-hour" Time Zone in English-speaking[*] North
America.

But have you yet found out about LHI?

You are, rather slowly, catching up - but you seem not to have taken due
advantage of the indications provided about where more can be learned.
[*] Scottish-speaking may be more accurate?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Apr 13 '06 #13
JRS: In article <11**********************@e56g2000cwe.googlegroups .com>
, dated Thu, 13 Apr 2006 09:10:26 remote, seen in
news:comp.lang.javascript, ma*******@gmail.com posted :
Based on everyone's input (instead of knitting) I came up with the
following and tested it for + - and GMT time.
Since you don't indicate exactly what it is supposed to do, it's not
easy to test it.
function getlocaleDate(inDate)
{
var curUTCDate = new Date(inDate);
Inefficient (if inDate is an Object), and may fail for years before 69.

Use var curUTCDate = new Date(+inDate);

// May cause problem in the future b/c of mm/dd/yy format
But no-one with any intelligence uses that format in data processing.
utcDate = (curUTCDate.getMonth()+1) + "/" + curUTCDate.getDate() +
"/" + curUTCDate.getFullYear();
Ditto.
utcHour = curUTCDate.getHours();
utcMinute = curUTCDate.getMinutes();

var currentDate = new Date(utcDate);

currentDate.setUTCHours(utcHour);
currentDate.setUTCMinutes(utcMinute);
return currentDate;
}

Anyone wants to test it? Might not be very efficient but it seems to be
working.


Setting a Date Object so that, read as a Civil Date/Time, it shows the
same numbers as a given UTC Date/Time, is an unreasonable thing to want
(and not always possible : 2006 April 2nd 02:30 AM did not exist in much
of NA, and 2006 October 29th 01:30 AM will occur twice).

One can code the I/O to/from a Date Object of Local Civil Date/Time and
of UTC Date/Time, just with standard Methods.

The sensible things that may also be needed are to convert, in each
direction, between a Date Object (which holds GMT milliseconds) and a
String or a set of Numbers representing the Civil Date/Time at a
location which is independent of that of the browser. Those who have
studied the newsgroup FAQ should be able to deal with those, both for
the general case (even LHI) and for specific cases such as NA-only.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 13 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Gord | last post: by
1 post views Thread by steven virnig | last post: by
2 posts views Thread by Joshua Moore | last post: by
2 posts views Thread by DGR | last post: by
7 posts views Thread by Jerome | last post: by
17 posts views Thread by Franc Zabkar | last post: by
2 posts views Thread by markric | last post: by
21 posts views Thread by rdemyan via AccessMonster.com | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.