javascript

How to Convert UNIX Timestamp to Date in Javascript


In this article, you will learn how to convert UNIX timestamp to date in Javascript.

Let’s say you have a variable named ‘a’ with the value of the current UNIX timestamp in seconds.

var a = Date.now();

In order to convert a UNIX timestamp to date, you can use the toLocaleString() method. In this example, you will be using British English, en-gbas a locale. The date will be formatted with day: "numeric", month: "2-digit", and year: "numeric". Example of formatted date is “18-02-2021”.

var a = Date.now();

var format = {
  day: "numeric",
  month: "2-digit",
  year: "numeric"
};

console.log(new Date(a).toLocaleString("en-gb", format));

Note: The toLocaleString() method functions by converting a Date object to a locality-sensitive representation of the date in string.


Share on social media

//