JS中的Date对象

Date对象

  • 在JS中使用一个Date对象来表示时间
  • 创建一个Date对象
  • 如果直接只用构造函数创建一个Date则会封装为当前代码执行的时间
  • 也可以指定一个时间;
  • 格式:月/日/年 时:分:秒
var d = new Date();
var d1 = new Date('04/20/2021 10:25:34 ');
console.log(d);
console.log(d1);
  • getFullYear()
  • 该方法可返回一个表示年份的 4 位数字。
console.log('年份 : '+d.getFullYear());
  • getMonth() 从对象中返回月份[0是一月以此类推]
console.log('月份 : '+d.getMonth());
  • getDay()
  • getDay() 方法可返回一周(0~6)的某一天的数字。
  • **注意: 星期天为 0, 星期一为 1, 以此类推。**
    
console.log('周几 : '+d.getDay());

- getDate()
-    从 Date 对象返回一个月中的某一天 (1 ~ 31)```javascript
console.log('天数 : '+d.getDate());
  • getHours()
  • 返回Date对象中的小时【0~23】
console.log('小时 : '+d.getHours());
  • getMinutes() 方法可返回时间的分钟字段。
console.log('分钟 : '+d.getMinutes());
  • getSeconds() 方法可返回时间的秒。
console.log('秒数 : '+d.getSeconds());
  • getMilliseconds() 方法可返回时间的毫秒。
console.log('毫秒 : '+d.getMilliseconds());
  • getTime() 返回1970年1月1日至今的毫秒数;
console.log('1970年1月1日至今的毫秒数 : '+d.getTime());
  • 获取当前时间戳
var time = Date.now();
console.log(time);
  • 可以用来测试代码执行的时间
var start = Date.now();
document.write('<br>');
var sum = 0;
for(i=1;i<10000;i++){

sum += i;

}
document.write(sum);
document.write('<br>');
var end = Date.now();
document.write( end-start);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wj18740503137

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值