AH.
AH.

Reputation: 813

Moment js Calendar Time from unix timestamp

These are moment js calendar output (from the momentjs website)

moment().subtract(10, 'days').calendar(); // 10/27/2017
moment().subtract(6, 'days').calendar();  // Last Tuesday at 9:10 PM
moment().subtract(3, 'days').calendar();  // Last Friday at 9:10 PM
moment().subtract(1, 'days').calendar();  // Yesterday at 9:10 PM
moment().calendar();                      // Today at 9:10 PM
moment().add(1, 'days').calendar();       // Tomorrow at 9:10 PM
moment().add(3, 'days').calendar();       // Thursday at 9:10 PM

I want to get same format output from my unix timestamp. I tried moment().unix(1509982956).calendar();, but that gives me error.

Upvotes: 2

Views: 755

Answers (1)

VincenzoC
VincenzoC

Reputation: 31482

Use moment.unix:

To create a moment from a Unix timestamp (seconds since the Unix Epoch), use moment.unix(Number).

console.log( moment.unix(1509982956).calendar() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>

instead of unix() that is only a getter function.

moment#unix outputs a Unix timestamp (the number of seconds since the Unix Epoch).

Upvotes: 2

Related Questions