[英]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.