14

I have a JSP page in which I am pulling timestamp stored in database as string which has the form Thu Aug 21 2014 22:09:23 GMT+0530 (India Standard Time).

Of Course, I am able to display it as it is in the page, however I was looking for a solution in javascript that would enable me convert this timestamp as per user's local timezone.

Is there a way to do this ? Or for such a timestamp it's not possible ? Any help is greatly appreciated, well my question may sound silly as I am still familiarizing myself with javascript.

Thanks

Raghvendra Kumar
  • 1,328
  • 1
  • 10
  • 26
  • http://stackoverflow.com/questions/6939685/get-client-time-zone-from-browser – caleb.breckon Aug 21 '14 at 19:12
  • Thanks for the link, but I think that's not what I am looking for, I don't want to detect user's timezone, rather I was looking for a way to convert my timestamp to a javascript date object which can be rendered as per the local timezone. – Raghvendra Kumar Aug 21 '14 at 19:33

3 Answers3

29

I figured it out myself and I am able to accomplish what I needed. Passing the timestamp from database to var new Date(timestamp) get it converted to local time, it takes care of timezone and offsets as well. Thanks for your time experts! :)

Raghvendra Kumar
  • 1,328
  • 1
  • 10
  • 26
4

You may try this

var timezone = new Date().getTimezoneOffset();

From MDN

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight savings time prevents this value from being a constant even for a given locale

Also here is an interesting article which may help you:- Auto detect a time zone with JavaScript

EDIT:

var d = new Date(myYear, myMonth, myDate);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Thanks for the link, but I think that's not what I am looking for, I don't want to detect user's timezone, rather I was looking for a way to convert my timestamp to a javascript date object which can be rendered as per the local timezone. – Raghvendra Kumar Aug 21 '14 at 19:33
  • @RaghvendraMishra:- Updated my ansswer. Hope thats what you are looking for! :) – Rahul Tripathi Aug 21 '14 at 19:37
  • Thanks Again @RT :- but I don't have year, month and date separately, all I have is a timestamp of the format `Thu Aug 21 2014 22:09:23 GMT+0530 (India Standard Time).` and was looking to directly convert this to a particular user's timezone settings. – Raghvendra Kumar Aug 21 '14 at 19:43
2

If you are going to use AngularJS, it will be much more easier and straightforward to convert from 1408648665 to Thu, 21 Aug 2014 19:17:45 GMT

    //html
    <div ng-controller="TimeCtrl">
      Date: {{timeStamp + '000' | date: 'medium'}}
    </div>

    //js
    var app = angular.module('myApp', []);
    app.controller('TimeCtrl', function time($scope) {
      $scope.timeStamp = 1408648665;
      console.log("hello");
    });

Fiddle: http://jsfiddle.net/hq0ry1vm/2/

admix
  • 1,752
  • 3
  • 22
  • 27