如何通过moment.js将unix时间转换为unix日期

时间:2017-02-15 11:52:01

标签: javascript date unix timestamp momentjs

有些像1487152419798这样的字符串是带有日期和时间戳的时间戳。时间 我需要像1487152419798这样的字符串,带有日期和时间戳。 00:00。

let timestamp = '1487152419798';
let date = moment(Number(timestamp)).format('YYYY-MM-DD');
let unixDate = moment.unix(date);  //don't work

我需要2017.02.15 00:00 in unix

2 个答案:

答案 0 :(得分:1)

您可以使用startOf("day")修改Moment实例,将所有时间字段设置为0.首先将timestamp转换为数字:

timestamp = Number(timestamp)

然后是:

如果你想要当地人:

let local = moment(timestamp).startOf("day");

或者如果你想要UTC:

let utc = moment.utc(timestamp).startOf("day");
// Note---------^^^^^



let timestamp = Number('1487152419798');
let local = moment(timestamp).startOf("day");
let utc = moment.utc(timestamp).startOf("day");
console.log("Date in local timezone: ", local.format());
console.log("Date in UTC: ", utc.format());

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

r = requests.post(url, headers=headers, data=json.dumps(payload), verify=False)

let timestamp = '1487152419798'; let date = new Date(Number(timestamp)); 应该为您提供所需的价值。