
I Built a Unix Timestamp Converter and Stepped on 3 JavaScript Date API Landmines
An API response returns 1709654400 . Can you tell what date that is at a glance? I couldn't. Every time, I'd google "Unix Timestamp converter" and paste it into some online tool. That tiny friction adds up, so I decided to build my own converter. Try it out: timestamp.puremark.app — zero-click timestamp conversion, right in your browser. I got it working, but not before stepping on 3 landmines in JavaScript's Date API. Landmine 1: new Date(1709654400) Returns 1970 I started confidently. Just pass the timestamp to new Date() . const date = new Date ( 1709654400 ); console . log ( date . toISOString ()); // => "1970-01-20T21:34:14.400Z" Way off. The cause was simple: JavaScript's Date expects milliseconds , but Unix timestamps are in seconds. You need to multiply by 1000. const date = new Date ( 1709654400 * 1000 ); console . log ( date . toISOString ()); // => "2024-03-05T00:00:00.000Z" But should users have to think about whether they're entering seconds or milliseconds? No. I implemen
Continue reading on Dev.to Webdev
Opens in a new tab




