我现在希望得到一个时间戳,但它只是日期的时间戳(因为我将使用这个值来查询,所以我需要精度)。
我以为我已经想出了解决的办法,但由于某种原因,我离开了一个月。
function dateToday() {
now = new Date(Date.now());
year = now.getFullYear();
month = now.getMonth();
day = now.getDate();
today = `${year}.${month}.${day}`
console.log(today);
date = new Date(today)
timestamp = date.getTime();
return timestamp
}它当前是2019.3.15,但函数正在记录2019.2.15。
发布于 2019-03-16 00:49:52
您可以使用setHours函数在单个步骤中完成此操作:
function dateToday() {
return new Date().setHours(0, 0, 0, 0);
}这会将小时、分钟、秒和毫秒设置为零,并返回数字时间戳。The documentation is here。
请注意,这是相对于用户的本地时区的。
发布于 2019-03-16 00:44:02
发布于 2019-03-16 00:44:02
在javascript月份中,从0开始表示一月
所以now.getMonth() + 1就是你所需要的。
https://stackoverflow.com/questions/55187124
复制相似问题