How to convert unix timestamp to date moment.js calendar

I have a Unix timestamp and am trying to convert it to 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 the calendar date, the message "Invalid date" appears in the window. Can someone help me?

+111
javascript datetime momentjs
Jan 6 '14 at 4:38
source share
8 answers

Using moment .js at your request:

 var dateString = moment.unix(value).format("MM/DD/YYYY"); 
+288
Jan 6 '14 at 5:16
source share

The UNIX timestamp is the number of seconds since 1970, so you need to convert it to a JS Date object:

 var date = new Date(unixTimestamp*1000); 
+34
Jan 06 '14 at 4:40
source share

Moment.js provides localized formats that you can use.

Here is an example:

 const moment = require('moment'); const timestamp = 1519482900000; const formatted = moment(timestamp).format('L'); console.log(formatted); // "02/24/2018" 
+13
Feb 24 '18 at 14:40
source share
 new moment(timeStamp,'yyyyMMddHHmmssfff').toDate() 
+4
Jul 01 '15 at 13:25
source share

I fixed it as this example.

 $scope.myCalendar = new Date(myUnixDate*1000); <input date-time ng-model="myCalendar" format="DD/MM/YYYY" /> 
0
Nov 06 '18 at 16:52
source share

It may be a little late, but for new problems like this, I use this code:

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

You can change the format to suit your needs, as well as add a time zone, for example like this:

 moment(timestamp, 'X').tz(timezone).format('lll'); 
0
Feb 26 '19 at 13:28
source share
 $(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 mode of forgiveness .

Although strict mode works better in most situations, forgiving mode can be very useful when the format of the string currently being transmitted may be different.

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

A common scenario where a pardon mode is useful is when a third-party API provides a date, and the date format for this API may change. Suppose the API starts by sending dates in the format "YYYY-MM-DD", and then changes to the format "MM / DD / YYYY".

In strict mode, the following code results in the display of "Invalid date":

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

In forgiveness mode using a format string, you get the 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); }); 
0
Apr 05 '19 at 17:45
source share

Only him,

 moment.unix(date).toDate(); 
0
May 23 '19 at 21:31
source share



All Articles