Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To convert UTC TIMESTAMP only, into local time, on the web with javascript? from http://openweathermap.org/

I am using the Weather APi found here: http://openweathermap.org/

The Sunrise and Sunset come back as numerical objects such as :

  1. 1425951068 (sunset okotoks, ab, can)
  2. 1425909686 (sunrise okotoks, ab, can)

I have tried all the methods i could find, but still no luck, as the majority of methods are looking for a complete UTC date, not just a UTC Time.

How can i convert this with javascript, into the user's local time zone?

like image 999
jordan Avatar asked Mar 09 '15 22:03

jordan


1 Answers

If all you want is local time (no date info) then Leo is right. I had incorrectly stated milliseconds but unix tick units are actually seconds so we must multiply by 1000 to construct a correct javascript Date.

var sec = 1425909686;
var date = new Date(sec * 1000);
var timestr = date.toLocaleTimeString();

console.log(date, timestr);

$("#d1").html(date);
$("#d2").html(timestr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1"></div>
<div id="d2"></div>
like image 146
Jasen Avatar answered Oct 04 '22 19:10

Jasen