日期对象的设置

关于的日期获取和设置

获取以 get开头 设置以 set开头

var currentDate = new Date() //获取当前的时间

获取年

console.log(currentDate.getFullYear()); //获取当前本地时间的年份

获取月

console.log(currentDate.getMonth()); //获取当前的月份 0 - 11 1月就是0 12月是11

获取日

console.log(currentDate.getDate());   //当月的第几日
console.log(currentDate.getDay());  //星期的第几日 星期天是第一天(从0开始算)

获取时

console.log(currentDate.getHours());//获取小时 24小时制 12点就是12 24点就是0

获取分

console.log(currentDate.getMinutes()); //获取分钟

获取秒

 console.log(currentDate.getSeconds());  //获取秒钟

加了utc就是获取utc时间 没有加就是获取本地时间

一般称为时间戳 这个一般作为唯一标识(有些时候数据过大我们还会在后面加随机数)
console.log(currentDate.getTime()); //获取距离1970年1月1日0时0分0秒的毫秒值

设置方法 Date是可变的

currentDate.setFullYear(2020) //设置年

currentDate.setDate(30) //设置当月的第几天
  
currentDate.setHours(10) 

currentDate.setMinutes(10)

currentDate.setSeconds(10)
console.log(currentDate);


currentDate.setTime(123456789) ; //设置离1970年1月1日0时0分0秒的毫秒值 
console.log(currentDate);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值