By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,044 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,044 IT Pros & Developers. It's quick & easy.

getting date in local time from GMT.

Claus Mygind
100+
P: 571
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

Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,390
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
100+
P: 571
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
Expert Mod 5K+
P: 5,390
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
100+
P: 571
Great coding advise. Just what I needed.
Feb 10 '10 #5

Post your reply

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