js中date日期

14 篇文章 0 订阅

日期对象的创建

当前日期为2020/02/11

//日期对象的创建
//当前日期
var oDate = new Date(); //2020/2/11
console.log(oDate); //Tue Feb 11 2020 16:56:08 GMT+0800 

//指定日期
var oDate1 = new Date("2019-10-3");
console.log(oDate1); //Thu Oct 03 2019 00:00:00 GMT+0800 

//指定日期
var oDate2 = new Date("2019/10/3");
console.log(oDate2); //Thu Oct 03 2019 00:00:00 GMT+0800 

//指定日期
//月份:0-11  0表示一月份   周几:0-6  0表示周日
var oDate3 = new Date(2019, 10, 3); //10对应11月
console.log(oDate3); //Sun Nov 03 2019 00:00:00 GMT+0800 

此外指定日期还可用以下方法:
new Date('02/11/2020')
new Date('2020-FEB-15')
new**加粗样式** Date('FEB, 11, 2020')
new Date('Feberuary, 11, 2020')
new Date('11 Feb 2020')
new Date('11, Feberuary, 2020') 

获取时间的一些方法

oDate.getDate() 返回一个月中的某一天 (1 ~ 31)
**oDate.getDay()**返回一周中的某一天 (0 ~ 6)
oDate.getMonth() 返回月份 (0 ~ 11)
oDate.getFullYear() 以四位数字返回年份
oDate.getHours() 返回当前小时(0-23)
oDate.getMinutes() 返回当前分钟 (0 ~ 59)
oDate.getSeconds() 返回当前秒(0 ~ 59)
oDate.getMillisenconds() 返回当前毫秒(0 ~ 999)

var oDate = new Date(); //2020/2/11

console.log(
   oDate.getFullYear(), //2020
    oDate.getMonth(), //1
    oDate.getDate(), // 11
    oDate.getDay() // 2
 );

console.log(
  oDate.getHours(),  //17
  oDate.getMinutes(), //57
  oDate.getSeconds(), //43
  oDate.getMilliseconds(), //65
  oDate.getTime() //1581415063065
);

时间的设置

oDate.setDate() 设置月中的某一天 (1 ~ 31)
oDate.setMonth() 设置月份 (0 ~ 11)
oDate.setFullYear() 设置年份(四位数)
oDate.setHours() 设置小时(0-23)
oDate.setMinutes() 设置分钟 (0 ~ 59)
oDate.setSeconds() 设置秒(0 ~ 59)
oDate.setMillisenconds() 设置毫秒(0 ~ 999)
oDate.setTime() 设置1970年1月1日至今的毫秒数

var oDate = new Date(); //2020/2/11

//设置年份
oDate.setFullYear(2021);
console.log(oDate); //Tue Aug 11 2021 21:32:28 GMT+0800 

//设置月份
oDate.setMonth(7);
console.log(oDate); // Tue Aug 11 2020 21:32:28 GMT+0800

//设置日期
oDate.setDate(40);
console.log(oDate); // Wed Mar 11 2020 21:32:28 GMT+0800 (中国标准时间)

注:这些参数如果超出了正常范围,会被自动折算。比如,如果月设为15,就折算为下一年的4月

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值