How to convert unix timestamp to calendar date moment.js

Andrew picture Andrew · Jan 6, 2014 · Viewed 259.8k times · Source

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?

Answer

Matt Johnson-Pint picture Matt Johnson-Pint · Jan 6, 2014

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