如何将unix时间戳转换为日历日期moment.js

时间:2022-08-22 19:44:52

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:

我有一个unix时间戳,我正在尝试将其转换为日历日期,如MM / DD / YYYY。到目前为止,我有这个:

$(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?

当我尝试打印日历日期时,窗口显示“无效日期”。谁能帮我吗?

4 个解决方案

#1


216  

Using moment.js as you asked:

你问的时候使用moment.js:

var dateString = moment.unix(value).format("MM/DD/YYYY");

#2


24  

UNIX timestamp it is count of seconds from 1970, so you need to convert it to JS Date object:

UNIX时间戳是从1970年开始的秒数,因此您需要将其转换为JS Date对象:

var date = new Date(unixTimestamp*1000);

#3


3  

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

#4


3  

Moment.js provides Localized formats which can be used.

Moment.js提供可以使用的本地化格式。

Here is an example:

这是一个例子:

const moment = require('moment');

const timestamp = 1519482900000;
const formatted = moment(timestamp).format('L');

console.log(formatted); // "02/24/2018"

#1


216  

Using moment.js as you asked:

你问的时候使用moment.js:

var dateString = moment.unix(value).format("MM/DD/YYYY");

#2


24  

UNIX timestamp it is count of seconds from 1970, so you need to convert it to JS Date object:

UNIX时间戳是从1970年开始的秒数,因此您需要将其转换为JS Date对象:

var date = new Date(unixTimestamp*1000);

#3


3  

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

#4


3  

Moment.js provides Localized formats which can be used.

Moment.js提供可以使用的本地化格式。

Here is an example:

这是一个例子:

const moment = require('moment');

const timestamp = 1519482900000;
const formatted = moment(timestamp).format('L');

console.log(formatted); // "02/24/2018"