自存,js关于日期相关的操作

new data();为创建一个新的时间

    const today = new Date();
    console.log(today)

可以获取对应的年月日 时分秒

    console.log(today.getFullYear(),today.getMonth()+1,today.getDate(),today.getHours(),today.getMinutes())

因为月份是从0开始算起,所以获取月份要加1

data转换成时间戳

在data类后面加上.time就变成时间戳了

通过时间戳判断某个日期是否为同一天

 const targetDate = new Date(timestamp);
    const today = new Date();
    const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime();
    
    const targetDateStart = new Date(targetDate.getFullYear(), targetDate.getMonth(), targetDate.getDate()).getTime();

如果todayStart 和targetDateStart 相等就是同一天否则则不是 

代码意思就是把根据年月日(不能添加时间和秒否则不一样)生成新的时间戳,然后比对时间戳判断是不是在同一天

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值