admin管理员组

文章数量:1313077

I am using the Weather APi found here: /

The Sunrise and Sunset e 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 plete UTC date, not just a UTC Time.

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

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

The Sunrise and Sunset e 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 plete UTC date, not just a UTC Time.

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

Share Improve this question edited Mar 11, 2015 at 1:05 ayla asked Mar 9, 2015 at 22:00 aylaayla 3,54612 gold badges50 silver badges75 bronze badges 5
  • What's your desired output? – Leo Commented Mar 11, 2015 at 1:16
  • Use javascript Date API. Those numbers are tics (milliseconds since the Epoch). – Jasen Commented Mar 11, 2015 at 1:17
  • Hey guys, thanks for the information - @Leo the desired output would be just the TIME of day – ayla Commented Mar 11, 2015 at 1:18
  • 1 (new Date(1425909686)).toLocaleTimeString() – Leo Commented Mar 11, 2015 at 1:20
  • 1 @Leo that is super incorrect. – ayla Commented Mar 11, 2015 at 2:43
Add a ment  | 

1 Answer 1

Reset to default 5

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1"></div>
<div id="d2"></div>

本文标签: