3

As the title reads, how can I convert a UNIX timestamp, e.g. 1458297862, into a Date object in Elm?

Date.fromString does not seem to accept it, and Date.fromTime gives the wrong answer.

bjrnt
  • 2,105
  • 3
  • 25
  • 32

2 Answers2

4

You can use Date.fromTime, but you have to multiply that value by 1000.

This gives you the date you'd expect:

Date.fromTime 1458297862000
Community
  • 1
  • 1
Chad Gilbert
  • 35,627
  • 3
  • 87
  • 96
2

I came across this answer and it didn't quite work in 0.18.

I found that Date.fromTime wouldn't accept arbitrary integers. The reason is that it's expecting floats, so you'd really want:

Date.fromTime (toFloat 1458297862000)
C Russo
  • 43
  • 7