![](/img/trans.png)
[英]How to get the timestamp of a specific previous/last time with Moment.js?
[英]How can the Moment.js get the timestamp?
在Moment.js中操作几天后,如何获取时间戳?
我尝试使用:
var a = moment().subtract(10, 'days').calendar().getTime()
获取时间戳,但失败了。
现在还不清楚“时间戳”是什么意思。
要获取自Unix时代以来的毫秒数,请使用moment().valueOf();
。 这对应于JS Date.getTime();
。
要获取自Unix时代以来的秒数,请使用moment().unix();
。
要获取小时/分钟/秒为数字,请使用moment().hour();
/ moment().minute();
/ moment().second();
。
要获取ISO 8601字符串(建议通过网络发送数据),请使用moment().toISOString();
(例如“ 2013-02-04T22:44:30.652Z”)。
要打印用户可读的时间,请使用moment().format();
,例如moment().format('LTS')
将返回包括秒在内的本地化时间(en-Us为“ 8:30:25 PM”)。
有关格式说明符,请参见moment.js-显示-格式 。
您可以使用moment().toDate()
来获取Javascript Date
对象。 然后,您可以使用getTime()
获得时间戳。
试试这个
moment().subtract(10, 'days').calendar().format('HH:MM:SS'); //get datetime
moment().subtract(1, 'days').calendar(); // Yesterday at 1:16 PM
calendar
没有方法getTime()
。 您可以按照以下方式格式化时间:
let format = 'YYYYMMDD';
let a = moment().subtract(10, 'days').format(format);
console.log(a)
经典日历格式:
const daysBefore = moment().subtract(10, 'days').calendar();
对于Unix时间戳(当前日期减去10天):
const daysBefore = moment().subtract(10, 'days').unix();
请记住,在减法后应用格式化,因为格式化返回的是字符串,而不是momentjs对象。
请尝试以下操作以获取时间戳:
moment().subtract(10, 'days')._d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.