Sunrise and sunset calculations for api weather machine

Sunrise and sunset calculations for api weather machine
0

#1

So i am doing the api weather machine. I want to display the users sunrise and sunset time. When i call this api, all i get is this number for sunrise: 1507473344. How do i convert this into a time? Is it a location, direction? I switched around two numbers to make sure i wasn’t accidentally posting my location or anything. Please help, thanks :slight_smile:


#2

A Unix timestamp?

You can convert it like this:


#3

unix_timestamp is not defined

doing this in repl.it

Its second from 1970 right?


#4

The number you get is indeed seconds. JS’s new Date() takes in a number of milliseconds. So to get your date:

let unix = 1507473344;
let date = new Date(unix*1000);

console.log(date);   // 2017-10-08T14:35:44.000Z

#5

How how would i get my sunrise and sunset in a readable format? Like for instance, 8:05am to 9:06pm


#6

EDIT: Or easier:
https://momentjs.com/


#7

Thanks, ill use this.