Convert a Unix Timestamp to Time in JavaScript

Convert a Unix timestamp to time in JavaScript

let unix_timestamp = 1549312452

// Create a new JavaScript Date object based on the timestamp

// multiplied by 1000 so that the argument is in milliseconds, not seconds.

var date = new Date(unix_timestamp * 1000);

// Hours part from the timestamp

var hours = date.getHours();

// Minutes part from the timestamp

var minutes = "0" + date.getMinutes();

// Seconds part from the timestamp

var seconds = "0" + date.getSeconds();

// Will display time in 10:30:23 format

var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);

console.log(formattedTime);

Convert UNIX timestamp to date time (javascript)

You have to multiply by 1000 as JavaScript counts in milliseconds since epoch (which is 01/01/1970), not seconds :

var d = new Date(timestamp*1000);

Reference

How to convert unix timestamp to calendar date moment.js

Using moment.js as you asked, there is a unix method that accepts unix timestamps in seconds:

var dateString = moment.unix(value).format("MM/DD/YYYY");

Unix timestamp with only month and year

You can simply create a new Date() using the month (converted to an index) and year from the string.

Here using an array of months to convert the named month to an index, and dividing the result by 1000 since javascript stores times in milliseconds while unix uses seconds.

const arr = [{ post_title: 'title', post_date: 'December 2021', }, { post_title: 'title', post_date: 'November 2021', }, { post_title: 'title', post_date: 'October 2021', }, { post_title: 'title', post_date: 'September 2021', }, { post_title: 'title', post_date: 'August 2021', }, { post_title: 'title', post_date: 'July 2021', }, { post_title: 'title', post_date: 'June 2021', },];

const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December',];

const result = arr.map((o) => {
const [month, year] = o.post_date.split(' ');

return {
...o,
unix_timestamp: new Date(year, months.indexOf(month)).valueOf() / 1000,
};
});

console.log(result);

Converting Unix timestamp to date time - different results for JS and Python

Your timestamp value has microsecond resolution.

51 years (since epoch) * 365 * 24 * 3600 will give roughly 1.6 billion seconds. Your value has additional 6 digits.

So in Python divide by 1000000 instead of 1000.

How to convert a Unix timestamp to 2000-01-01 or 2000-05-24 20:00:00 format or reverse in Deno?

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.

Converting unix timestamp to Milliseconds in luxon.js

When a time is passed to fromISO, the current date is used. To get the time in milliseconds, parse it to a DateTime and subtract a DateTime for the start of the day, e.g.

let DateTime = luxon.DateTime;

function timeToMs(time) {
let then = DateTime.fromISO(time);
let now = DateTime.fromISO("00:00");
return then - now;
}

console.log(timeToMs('05:00'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.4.0/luxon.min.js"></script>

convert a unix timestamp to localtime

Try this:

x: new Date(this.unixtimearray[i].time*1000)

If this.unixtimearray[i].time is a string:

x: new Date(parseInt(this.unixtimearray[i].time)*1000)


Related Topics



Leave a reply



Submit