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. 13 16018
<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 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
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.
"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)
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)
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.
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.
"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.
"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.
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gord |
last post by:
Many scripts and calendars call client side system time in order to
make presentations.
However, the client's time may be improperly set, if set at all,
and/or the relevant time may be from...
|
by: steven virnig |
last post by:
We have an application group that wants to pull date from SQL Server and write it to text file on the server. They want the file format to be 12100_YYYMMDDHHMM.fr1 for one set of data,...
|
by: Joshua Moore |
last post by:
Does anyone know how I'd set the local computer's date/time in C#?
Thanks in advance,
Joshua Moore
|
by: DGR |
last post by:
I have a time value in the number of seconds since 1970. I need to convert
this into a date/time format such as MM/DD/YYYY HH:MM or something similar.
This value needs to display in terms of the...
|
by: Jerome |
last post by:
Hallo,
I know a lot has already been told about date/time fields in a database but
still confuses me, specif when dealing with SQLserver(Express).
It seems that sqlserver only accepts the date in...
|
by: Franc Zabkar |
last post by:
My D-Link DSL-302G modem/router has a real-time clock whose settings
are volatile. To avoid hand keying the date/time via the modem's JS
interface, I wonder if there is a way to copy the JS code to...
|
by: markric |
last post by:
I recently posted this message to
microsoft.public.dotnet.framework.aspnet.webservices, but received no
response, so I'm trying here in this group. I could really use some
feedback.
I'm working...
|
by: rdemyan via AccessMonster.com |
last post by:
Is there a way to get the internet date/time. I saw an article that uses
WinSock, but WinSock doesn't seem to be available in Access.
I want to verify that the date/time on the local PC running...
|
by: Kevin Frey |
last post by:
Our database stores all date-time values in UTC format. When I display this
data in a GridView (using a HyperLinkField) I'd like to display the
equivalent local-time instead.
It seems to me that...
|
by: MBMSOFT |
last post by:
Any idea How to prevent from different date/time format on different pc
I've heard that I should create my own system time function in VBA which will not depend from the local pc system date/time...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |