Andrew
Andrew

Reputation: 3589

How to convert unix timestamp to calendar date moment.js

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?

Upvotes: 213

Views: 473490

Answers (16)

Ahmed Hdeawy
Ahmed Hdeawy

Reputation: 84

I tried multiple times to use the unix method but it doesn't work properly. the one that worked fine is convert the unix timestamp to Number and then use the normal moment like moment(Number(value))

Upvotes: 0

Ricardo
Ricardo

Reputation: 19

Leaving this here since I didn't find a specific answer to my particular situation.

The timestamp I was trying to format was 1676665996075, but I had it stored in a property as a string value, so it was actually "1676665996075".

I tried doing moment.unix(property.value) but I kept getting Wed Nov 07 55100 10:50:21 GMT-0300. As you can see, the date was coming out all wrong.

It worked fine when I started transforming it into a number first, and didn't even need to call .unix().

moment(Number(property.value)) did the trick for me. It came out as Fri Feb 17 2023 12:28:11 GMT-0300.

So, remember that the timestamp value needs to be a number.

Upvotes: 1

Using: moment.unix(timeSpan).format(DD/MM/YYYY')

Upvotes: 0

jack blank
jack blank

Reputation: 5195

I had a timestamp like 1668919452.

Doing this got me the proper result : {moment.unix(created).format("l")}

I added the .unix method.

Before that, the time stamps were registered as 1970s

Upvotes: 3

First you can convert timestamp to date object with js

const date = new Date(timestamp)

Then you can use momentjs to format it the way you want

const formatDate = moment(date).format("DD/MM/YYYY")

Upvotes: 1

Eduardo Sanchez G
Eduardo Sanchez G

Reputation: 379

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

Ref: https://momentjs.com/docs/#/parsing/string/

Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

example

moment('2022-12-03').format('YYYY-MM-DD'); //avoid use this because inconsistentcy
moment('2022-12-03','YYYY-MM-DD').format('YYYY-MM-DD'); //use this instead
moment('1670000400','X').format('YYYY-MM-DD'); //use this instead (timestamp)
moment('03-2022-12','DD-YYYY-MM').format('YYYY-MM-DD'); //UNCOMMON CASE

Upvotes: 15

Rishabh Mehrotra
Rishabh Mehrotra

Reputation: 645

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

Upvotes: 0

Vincent
Vincent

Reputation: 16216

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

Upvotes: 5

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241525

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

Upvotes: 541

Bill Zelenko
Bill Zelenko

Reputation: 2858

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')
    }

Upvotes: 1

Only it,

moment.unix(date).toDate();

Upvotes: 14

devon kassian
devon kassian

Reputation: 11

$(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);
});

Upvotes: 1

KHACHORNCHIT
KHACHORNCHIT

Reputation: 2320

I fixed it like this example.

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

Upvotes: 0

Benny Code
Benny Code

Reputation: 54812

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"

Upvotes: 36

Mohamed.Abdo
Mohamed.Abdo

Reputation: 2200

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

Upvotes: 5

Cris
Cris

Reputation: 13351

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

Upvotes: 49

Related Questions