473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

getting date in local time from GMT.

Claus Mygind
571 512MB
I have a date variable which I receive in the following format:

"2010-02-07T16:49:26Z"

This is a GMT timestamp on my Garmin GPS. I would like the result in my own local time, based on the computer I am using.

What would be some basic code to get this done?
Feb 8 '10 #1

✓ answered by gits

try the following:
Expand|Select|Wrap|Line Numbers
  1. var s = '2010-02-09T16:49:26Z';
  2.  
  3. s = s.replace(/[-|T|Z]/g, ' ');
  4.  
  5. var d = new Date(s);
  6.  
  7. alert(d);
  8.  
kind regards

4 2679
gits
5,390 Expert Mod 4TB
just use some string operations or a regExp to format that string ... how should it look like?

kind regards
Feb 9 '10 #2
Claus Mygind
571 512MB
In the code below date information is store in the variable cVal.

I separate the date from the time (perhaps not really needed).

Then I use regExp to replace - and : with a comma and space.

Then I combine date and time again in cDate.

My problem is this cDate is a string variable with " " around it. each value needs to be a number not alpahnumeric. As a result I get invalid date.

How do I turn these into numbers?

Expand|Select|Wrap|Line Numbers
  1.             var cDate = cVal.slice(0, cVal.indexOf("T") );
  2.             var cTime = cVal.slice( cVal.indexOf("T")+1, cVal.indexOf("Z") ); 
  3.             cDate = cDate.replace( /-/g, ", ");
  4.             cTime = cTime.replace( /:/g, ", ");
  5.             cDate += ", "+cTime
  6.  
  7.             var dt = new Date( cDate );
  8.  
  9.             cTime = getLocalTimeFromGMT(dt)
  10.  
  11.  
  12. function getLocalTimeFromGMT(sTime){
  13.   var dte = new Date(sTime);
  14.   dte.setTime(dte.getTime()
  15.     - dte.getTimezoneOffset()*60*1000);
  16. //  document.write(dte.toLocaleString());
  17. }
  18.  
  19.  
Feb 9 '10 #3
gits
5,390 Expert Mod 4TB
try the following:
Expand|Select|Wrap|Line Numbers
  1. var s = '2010-02-09T16:49:26Z';
  2.  
  3. s = s.replace(/[-|T|Z]/g, ' ');
  4.  
  5. var d = new Date(s);
  6.  
  7. alert(d);
  8.  
kind regards
Feb 9 '10 #4
Claus Mygind
571 512MB
Great coding advise. Just what I needed.
Feb 10 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: | last post by:
I have a script... ----- <SCRIPT language="JavaScript" type="text/javascript"> <!-- function makeArray() { for (i = 0; i<makeArray.arguments.length; i++) this = makeArray.arguments; } ...
6
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By "common" I mean at the least ones described in this...
2
by: Tatiana Zadiraka | last post by:
I use DB2 8.1 with FixPack 5. In command line for sql I get all DATE columns only in MM-DD-YYYY format. Of course, DATE('20-12-2003') gives me an error SQL0181N The string representation of a...
3
by: Jon Davis | last post by:
The date string: "Thu, 17 Jul 2003 12:35:18 PST" The problem: // this fails on PST DateTime myDate = DateTime.Parse("Thu, 17 Jul 2003 12:35:18 PST"); Help? Jon
3
by: Ilja Booij | last post by:
Hi all, I have some trouble with the following: I'm getting a time string, in YYYY-MM-DD HH:mm:ss format, which I need to translate into a string with DD-Mon-YYYY HH:mm:ss +HHMM, where the...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
4
by: franco | last post by:
I am a beginner in programming. How can I display the system date - 5 December 2005 in my webpage.
13
by: maflatoun | last post by:
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...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
11
by: shsandeep | last post by:
I used the following query to retrieve the date in dd-mon-yyyy format. db2 => SELECT RTRIM(CHAR(DAY(COVG_TYP_STRT_DT))) || '-' || RTRIM(MONTHNAME(COVG_TYP_STRT_DT)) || '-' ||...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.