获取当前日期前三天和前七天的时间戳的方法
一、获取当前日期的方法
复制代码var currentDate = new Date();var currentTimestamp = currentDate.getTime();console.log(currentTimestamp);
以上代码中,我们使用new Date()获取当前日期的Date对象,然后使用getTime()方法获取该日期的时间戳。
二、计算时间获取时间戳的方法
复制代码function getBeforeTimestamp(n) { var currentDate = new Date(); var beforeDate = new Date(currentDate.getTime() - n * 24 * 60 * 60 * 1000); var beforeTimestamp = beforeDate.getTime(); return beforeTimest}console.log(getBeforeTimestamp(3)); // 前三天的时间戳console.log(getBeforeTimestamp(7)); // 前七天的时间戳以上代码中,我们定义了一个名为getBeforeTimestamp的函数,该函数接受一个参数n表示前几天。我们首先获取当前日期的时间戳,然后通过减去n天的毫秒数(24小时 * 60分钟 * 60秒 * 1000毫秒)来得到前几天的日期,再通过getTime()方法获取该日期的时间戳。
Disclaimers:The materials on this website are collected and sorted from the Internet. The copyright of the works belongs to the author. If it infringes your copyright, please send an email to us

简体中文
English
Article comments