Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > UTC date and time to local

Reply
Thread Tools

UTC date and time to local

 
 
maflatoun@gmail.com
Guest
Posts: n/a
 
      04-09-2006
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.

 
Reply With Quote
 
 
 
 
Hal Rosser
Guest
Posts: n/a
 
      04-09-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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...in:ate#Methods
Hope this helps


 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      04-09-2006
JRS: In article <(E-Mail Removed) .com>
, dated Sat, 8 Apr 2006 22:00:21 remote, seen in
news:comp.lang.javascript, (E-Mail Removed) 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.
 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      04-09-2006

"Hal Rosser" <(E-Mail Removed)> wrote in message
news:jO1_f.4019$(E-Mail Removed)...
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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...in:ate#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)



 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      04-10-2006
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)
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      04-10-2006
JRS: In article <Xns97A16A5F1A79Eeejj99@194.109.133.242>, dated Mon, 10
Apr 2006 08:27:23 remote, seen in news:comp.lang.javascript, Evertjan.
<(E-Mail Removed)> 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 &#177; 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.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      04-10-2006
JRS: In article <nNg_f.2514$(E-Mail Removed)>, dated
Sun, 9 Apr 2006 19:32:44 remote, seen in news:comp.lang.javascript, Hal
Rosser <(E-Mail Removed)> posted :

>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) 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.
 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      04-11-2006

"Dr John Stockton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> JRS: In article <nNg_f.2514$(E-Mail Removed)>, dated
> Sun, 9 Apr 2006 19:32:44 remote, seen in news:comp.lang.javascript, Hal
> Rosser <(E-Mail Removed)> posted :
>
> >> <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed) 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.


 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      04-12-2006

"Dr John Stockton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> JRS: In article <nNg_f.2514$(E-Mail Removed)>, dated
> Sun, 9 Apr 2006 19:32:44 remote, seen in news:comp.lang.javascript, Hal
> Rosser <(E-Mail Removed)> posted :
>
> >> <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed) 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.
>


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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert UTC DateTime to Local Time (Not My Local Time) Jonathan Wood ASP .Net 1 11-01-2009 12:08 AM
Time conversion between UTC and local time andreas.profous@googlemail.com Python 2 02-09-2008 02:54 AM
convert UTC to local time(system time) and vice vesra in VB.NET deepak ASP .Net 1 02-06-2008 02:34 PM
convert time string in UTC to time in local time davelist@mac.com Python 1 03-11-2007 12:57 AM
Displaying a UTC date-time from DB in local-time in a GridView Kevin Frey ASP .Net 0 11-30-2006 03:18 AM



Advertisments