Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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");
                I suppose it is according to the question. But I think a calendar date in moment.js looks like this.... moment(new Date(item.date)).calendar()
– Ian Warburton
                Jan 14, 2015 at 21:35
                @IanWarburton - You wouldn't need the new date object in there.  The calendar function is nice (docs here), but not in the format that the OP asked for.
– Matt Johnson-Pint
                Jan 14, 2015 at 21:51
                It's worth noting why the question fails but this works. The reason is because monent(number) expects a unix timestamp in milliseconds (which is consistent with the Date object), whereas the unix timestamp is in seconds by default. So you could also do moment(value*1000) instead of moment.unix(value), but using unix is clearer.
– icc97
                Mar 11, 2020 at 10:09

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);
                Yes, that was the solution with the latest version of moment, or at least the one i got from npm recently. even though i got the .unix() from moment i had to * 1000 when instantiating it again. for instance: moment( moment().unix() * 1000 )
– kroe
                Nov 19, 2015 at 2:53

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
                Just to add to this answer (which is the most correct answer on here). Use lowercase x for ms unix timestamp. Documentation here
– JonTroncoso
                May 25, 2020 at 0:44
                While this code may answer the question, it would be better to include some context, explain how it works, and describe when to use it. Code-only answers are not useful in the long run. Additionally, your code is not properly formatted.
– ryanyuyu
                Aug 31, 2015 at 18:53
$(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);

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

While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 9, 2020 at 21:39

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

Welcome to stackoverflow. This question is asked more than 8 years ago and it has an accepted answer. Please add some details about the reason you are adding a new answer – MD Zand Nov 29, 2022 at 20:25

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.