How to convert unix timestamp to calendar date moment.js

JavascriptDatetimeMomentjs

Javascript Problem Overview


I have a unix timestamp, and I'm trying to convert it into a calendar date such as MM/DD/YYYY. So far, I have this:

$(document).ready(function() {
  var value = $("#unixtime").val(); //this retrieves the unix timestamp
  var dateString = moment(value).calendar(); 
  alert(dateString);
});

When I try to print out the calendar date, the window says "Invalid date". Can anyone help me out?

Javascript Solutions


Solution 1 - Javascript

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");

Solution 2 - Javascript

UNIX timestamp it is count of seconds from 1970, so you need to convert it to JS Date object:

var date = new Date(unixTimestamp*1000);

Solution 3 - Javascript

Moment.js provides Localized formats which can be used.

Here is an example:

const moment = require('moment');

const timestamp = 1519482900000;
const formatted = moment(timestamp).format('L');

console.log(formatted); // "02/24/2018"

Solution 4 - Javascript

Only it,

moment.unix(date).toDate();

Solution 5 - Javascript

Might be a little late but for new issues like this I use this code:

moment(timestamp, 'X').format('lll');

You can change the format to match your needs and also add timezone like this:

moment(timestamp, 'X').tz(timezone).format('lll');

Solution 6 - Javascript

new moment(timeStamp,'yyyyMMddHHmmssfff').toDate()

Solution 7 - Javascript

moment(1454521239279).toDate()
moment(1454521239279).format()

Solution 8 - Javascript

I fixed it like this example.

$scope.myCalendar = new Date(myUnixDate*1000);
<input date-time ng-model="myCalendar" format="DD/MM/YYYY" />

Solution 9 - Javascript

$(document).ready(function() {
    var value = $("#unixtime").val(); //this retrieves the unix timestamp
    var dateString = moment(value, 'MM/DD/YYYY', false).calendar(); 
    alert(dateString);
});

There is a strict mode and a Forgiving mode.

While strict mode works better in most situations, forgiving mode can be very useful when the format of the string being passed to moment may vary.

In a later release, the parser will default to using strict mode. Strict mode requires the input to the moment to exactly match the specified format, including separators. Strict mode is set by passing true as the third parameter to the moment function.

A common scenario where forgiving mode is useful is in situations where a third party API is providing the date, and the date format for that API could change. Suppose that an API starts by sending dates in 'YYYY-MM-DD' format, and then later changes to 'MM/DD/YYYY' format.

In strict mode, the following code results in 'Invalid Date' being displayed:

moment('01/12/2016', 'YYYY-MM-DD', true).format()
"Invalid date"

In forgiving mode using a format string, you get a wrong date:

moment('01/12/2016', 'YYYY-MM-DD').format()
"2001-12-20T00:00:00-06:00"

another way would be

$(document).ready(function() {
    var value = $("#unixtime").val(); //this retrieves the unix timestamp
    var dateString = moment.unix(value).calendar(); 
    alert(dateString);
});

Solution 10 - Javascript

This function creates date from timestamp:

    function formatDateTime(dateString) {
        const parsed = moment(new Date(dateString))

        if (!parsed.isValid()) {
            return dateString
        }

        return parsed.format('MMM D, YYYY, HH:mmA')
    }

Solution 11 - Javascript

moment(timestamp).format('''any format''')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - JavascriptMatt Johnson-PintView Answer on Stackoverflow
Solution 2 - JavascriptCrisView Answer on Stackoverflow
Solution 3 - JavascriptBenny NeugebauerView Answer on Stackoverflow
Solution 4 - JavascriptDanillo Leão LopesView Answer on Stackoverflow
Solution 5 - JavascriptEduardo Sanchez GView Answer on Stackoverflow
Solution 6 - JavascriptMohamed.AbdoView Answer on Stackoverflow
Solution 7 - JavascriptVincentView Answer on Stackoverflow
Solution 8 - JavascriptKhachornchit SongsaenView Answer on Stackoverflow
Solution 9 - Javascriptdevon kassianView Answer on Stackoverflow
Solution 10 - JavascriptBill ZelenkoView Answer on Stackoverflow
Solution 11 - JavascriptRishabh MehrotraView Answer on Stackoverflow