在JavaScript中,时间戳是指自1970年1月1日00:00:00 UTC到现在的毫秒数。如果你想要获取昨天的时间戳,可以通过以下步骤实现:
// 获取当前时间的时间戳
let now = new Date();
let nowTimestamp = now.getTime();
// 计算一天的毫秒数
let oneDay = 24 * 60 * 60 * 1000;
// 计算昨天的时间戳
let yesterdayTimestamp = nowTimestamp - oneDay;
console.log("当前时间戳:", nowTimestamp);
console.log("昨天的时间戳:", yesterdayTimestamp);
new Date()
创建一个表示当前时间的Date对象,然后使用getTime()
方法获取其时间戳。24 * 60 * 60 * 1000
。通过上述方法,你可以轻松地在JavaScript中获取昨天的时间戳,并应用于各种实际场景中。
领取专属 10元无门槛券
手把手带您无忧上云