473,839 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 formatToLocalTi meDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezo neOffset();
if(offSet < 0) {
inDateMod.setMi nutes(inDateMod .getMinutes()+o ffSet);
} else {
inDateMod.setMi nutes(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 16183
<ma*******@gmai l.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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 formatToLocalTi meDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezo neOffset();
if(offSet < 0) {
inDateMod.setMi nutes(inDateMod .getMinutes()+o ffSet);
} else {
inDateMod.setMi nutes(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.setUTCH ours(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 formatToLocalTi meDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezo neOffset();
if(offSet < 0) {
inDateMod.setMi nutes(inDateMod .getMinutes()+o ffSet);
} else {
inDateMod.setMi nutes(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************ **********@v46g 2000cwv.googleg roups.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 formatToLocalTi meDate(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.getTimezo neOffset();
if(offSet < 0) {
inDateMod.setMi nutes(inDateMod .getMinutes()+o ffSet);
} else {
inDateMod.setMi nutes(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().getTimez oneOffset())

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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 9 '06 #4

"Hal Rosser" <hm******@bells outh.net> wrote in message
news:jO******** *********@bigne ws7.bellsouth.n et...
<ma*******@gmai l.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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.setUTCH ours(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 formatToLocalTi meDate(inDate) {
var inDateMod = new Date(inDate);
inDateMod.setUT CHours( 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.javas cript:
inDateMod = new Date(+inDate +- 6e4*new Date().getTimez oneOffset())

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.10 9.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.javas cript:
inDateMod = new Date(+inDate +- 6e4*new Date().getTimez oneOffset())

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.demo n.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.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******@bells outh.net> posted :
<ma*******@gmai l.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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 formatToLocalTi meDate(inDate) {
var inDateMod = new Date(inDate);
inDateMod.setUT CHours( 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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 10 '06 #8

"Dr John Stockton" <jr*@merlyn.dem on.co.uk> wrote in message
news:Rl******** ******@merlyn.d emon.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******@bells outh.net> posted :
<ma*******@gmai l.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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.dem on.co.uk> wrote in message
news:Rl******** ******@merlyn.d emon.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******@bells outh.net> posted :
<ma*******@gmai l.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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 formatToLocalTi meDate(inDate) {
var inDateMod = new Date(inDate);
inDateMod.setUT CHours( 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2313
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 another time zone. It would be of great value if the time of some central source could be drawn, in the form of a ".js" file, into the script to avoid dependence on the client's date/time.
1
4438
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, 12100_YYYMMDDHHMM.fr2 for a second set...and so on. The '12100' is fixed, but the rest of the file name will always have to include the system date/time. Is there an easy way to do this within a DTS package (when writing to the output file)? I would really...
2
4186
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
2
6666
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 local time/time zone on which my program is running. Are there any built-in functions that do this conversion that take time zone into account? I'm not sure where to find any VB.NET time-related functions. Thanks for any help you can provide.
7
4245
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 a "yyyyMMdd" format? (difference between Express and MSDE2000A ?) What is the one and only true way to deal with this problem in VB2005: Local settings are Dutch (Belgium) ; thus date is in "dd/MM/yy" (or perhaps dd/MM/yyyy) and time in...
17
3379
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 the hard drive and modify it to automatically retrieve the PC's date/time. I could then add the hacked JS page to my browser's bookmarks. I've already successfully modified and adapted other modem menus, but I don't know how to go about this...
2
1427
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 on a commercial application that will have data, web service, and client application tiers. I have no control over where any of these systems might be deployed, and it's fairly likely that users running the client application will be in different...
21
16609
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 my application is correct, so I was hoping I could get the Internet date/time. I understand that there is a NTP protocol to do this. Thanks. --
0
1036
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 this would have been trivially easy if there was a formatting string that would make the conversion prior to formatting. But alas there appears to be no such capability. Without this approach, I'm not sure where I would inject the "ToLocalTime"...
3
4692
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 setting Regards
0
9855
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10586
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10293
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7828
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.