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,552
  • 3
  • 27
  • 38

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
  • 36,115
  • 4
  • 89
  • 97
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
  • 53
  • 7