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

10,101

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>

Share:
10,101
jordan
Author by

jordan

Updated on September 18, 2022

Comments

  • jordan
    jordan over 1 year

    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?