How to convert timestamp string to local time using javascript?

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

Of course, I can display it as it is on the page, however I was looking for a solution in javascript that would allow me to convert this timestamp according to the user local timezone.

Is there any way to do this? Or is it impossible for such a timestamp? Any help is much appreciated and my question may sound silly since I am still familiar with javascript.

thanks

+5
source share
4 answers

I realized this myself, and I can do what I need. Passing a timestamp from the database to var new Date(timestamp) converted to local time, it also takes care of the time zone and offsets. Thank you experts for your time! :)

+8
source

I think this is what you are looking for: https://gist.github.com/kmaida/6045266

+1
source

You can try this

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

From MDN

The time zone offset is the difference in minutes between UTC and local time. Please note that this means that the offset is positive if the local time zone 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 saving time does not allow this value to be constant even for a given language

Also here is an interesting article that can help you: Automatically detect the time zone using JavaScript

EDIT:

 var d = new Date(myYear, myMonth, myDate); d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 ); 
+1
source

If you are going to use AngularJS, it will be much simpler and easier 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/

+1
source

Source: https://habr.com/ru/post/1200875/


All Articles