Benjamin So
Benjamin So

Reputation: 97

formatting date, time and temperature after loading DarkSky using Javascript

I am working on my own project which is a website for the weather forecast. However, I have some issues with date, time and temperature format after loading JSON from DarkSky. Bellow are my data after having a JSON from DarkSky:

apparentTemperatureHigh: 88.48
​​​​
apparentTemperatureHighTime: 1527627600
​​​​
apparentTemperatureLow: 69.66
​​​​
apparentTemperatureLowTime: 1527674400
​​​​
apparentTemperatureMax: 88.48
​​​​
apparentTemperatureMaxTime: 1527627600
​​​​
apparentTemperatureMin: 68.31
​​​​
apparentTemperatureMinTime: 1527573600
​​​​
cloudCover: 0.88
​​​​
dewPoint: 68.69
​​​​
humidity: 0.83
​​​​
icon: "fog"
​​​​
moonPhase: 0.51
​​​​
ozone: 318.12
​​​​
precipIntensity: 0.0003
​​​​
precipIntensityMax: 0.0018
​​​​
precipIntensityMaxTime: 1527649200
​​​​
precipProbability: 0.15
​​​​
precipType: "rain"
​​​​
pressure: 1016.16
​​​​
summary: "Foggy in the morning."
​​​​
sunriseTime: 1527587293
​​​​
sunsetTime: 1527640035
​​​​
temperatureHigh: 84.29
​​​​
temperatureHighTime: 1527627600
​​​​
temperatureLow: 68.49
​​​​
temperatureLowTime: 1527674400
​​​​
temperatureMax: 84.29
​​​​
temperatureMaxTime: 1527627600
​​​​
temperatureMin: 67.36
​​​​
temperatureMinTime: 1527573600
​​​​
time: 1527566400
​​​​
uvIndex: 7
​​​​
uvIndexTime: 1527613200
​​​​
visibility: 6.25
​​​​
windBearing: 146
​​​​
windGust: 11.99
​​​​
windGustTime: 1527645600
​​​​
windSpeed: 2.44

So, I do not know how can I convert from time which is 1527566400 to a time that I can read. In addition, I confuse temperatureHighTime and temperatureLowTime. Could you give me a hint how to format those values into readable value? I will appreciate any help.

Upvotes: 0

Views: 1008

Answers (2)

HankCa
HankCa

Reputation: 9669

const moment=require('moment')   // in the shell 'npm i moment' to install it

const dateTime=moment(<unix time in seconds>, 'X')

# Where 'X' is the format shortcut for seconds

console.log(dateTime.format("YY-MM-DD HH:mm:ss")    // 2019-04-13 10:56:12

For celcius v farenheit, change the units. For https://www.npmjs.com/package/dark-sky pass .units('ci') for scientific units.

Upvotes: 0

hitecherik
hitecherik

Reputation: 442

The time given looks like it's in UNIX time - the number of seconds since 00:00 UTC on 1 January 1970.

Most high-level programming languages have a function for converting from UNIX time to a more readable value - here's a previous answer that gives instructions.

Upvotes: 1

Related Questions