getting date in local time from GMT.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    getting date in local time from GMT.

    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?
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    just use some string operations or a regExp to format that string ... how should it look like?

    kind regards

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      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?

      Code:
      			var cDate = cVal.slice(0, cVal.indexOf("T") );
      			var cTime = cVal.slice( cVal.indexOf("T")+1, cVal.indexOf("Z") ); 
      			cDate = cDate.replace( /-/g, ", ");
      			cTime = cTime.replace( /:/g, ", ");
      			cDate += ", "+cTime
      
      			var dt = new Date( cDate );
      
      			cTime = getLocalTimeFromGMT(dt)
      
      
      function getLocalTimeFromGMT(sTime){
        var dte = new Date(sTime);
        dte.setTime(dte.getTime()
          - dte.getTimezoneOffset()*60*1000);
      //  document.write(dte.toLocaleString());
      }

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        try the following:
        Code:
        var s = '2010-02-09T16:49:26Z';
        
        s = s.replace(/[-|T|Z]/g, ' ');
        
        var d = new Date(s);
        
        alert(d);
        kind regards

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          Great coding advise. Just what I needed.

          Comment

          Working...