How to obtain the current date

复制代码var currentDate = new Date();var currentTimestamp = currentDate.getTime();console.log(currentTimestamp);

In the above code, we usenew Date()获取当前日期的Date对象,然后使用getTime()方法获取该日期的时间戳。

Second, the method of calculating the time to obtain the timestamp

复制代码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)); // 前七天的时间戳

In the above code, we define a system namedgetBeforeTimestamp的函数,该函数接受一个参数n表示前几天。我们首先获取当前日期的时间戳,然后通过减去n天的毫秒数(24小时 * 60分钟 * 60秒 * 1000毫秒)来得到前几天的日期,再通过getTime()方法获取该日期的时间戳。