1

I am new in Deno. I want to know how can I convert Unix timestamp like 1646245390158 to 2000-01-01 or 2000-05-24 20:00:00 format or vice versa?

jps
  • 20,041
  • 15
  • 75
  • 79
Hasani
  • 3,543
  • 14
  • 65
  • 125
  • Does this answer your question? [Convert a Unix timestamp to time in JavaScript](https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript) – Zwiers Mar 02 '22 at 18:48
  • @Zwiers: I tried the accepted answer of that question but it gives me the wrong time and doesn't work properly. – Hasani Mar 02 '22 at 18:59
  • 1
    `1646245390158`: That's not a UNIX timestamp: UNIX timestamps represent seconds since the epoch, but that value represents milliseconds. – jsejcksn Mar 02 '22 at 23:35
  • @jsejcksn: Do you know what should I call it? – Hasani Mar 03 '22 at 19:05
  • 1
    @WDR That’s a great question! I don’t know if it has a technical name, but I think if you call it “UNIX milliseconds” that everyone should reasonably understand exactly what you’re talking about, and anyone with experience using JS Dates would know you mean the numeric value of `date.getTime()`/`Date.now()`. – jsejcksn Mar 03 '22 at 21:06

1 Answers1

1

Aside from the standard Javascript ways, there is a date/time library for Deno called Ptera, that can be used as follows:

import { datetime } from "https://deno.land/x/ptera/mod.ts";

const dt = datetime("2000-05-24 20:00:00");
console.log(dt.format("X"));  // X for Unix timestamp in seconds 959198400
console.log(dt.format("x"));  // x for "Unix timestamp" in milliseconds  959198400000


const dt2 = datetime(1646245390158);
console.log(dt2.format("YYYY-MM-dd HH:mm:ss"));  // output: 2022-03-02 19:23:10

A UNIX timestamp is the number of seconds since 1970-01-01 00:00:00 UTC, the Javascript timestamp is in milliseconds and sometimes in documentations, they also call this as a UNIX timestamp or UNIX Epoch time.

A detailed reference about the formatting options is available here.

jps
  • 20,041
  • 15
  • 75
  • 79