JS new Date( )获取时间和处理时间方法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

</body>
<script>
    //定义一个新的new Date()对象
    let date = new Date();
    window.console.log(typeof date); //对象
    window.console.log(date); //获取当前时间点
    //获取某一个时间段
    let dateBySelf = "2019-01-02";
    let dateDeal = new Date(dateBySelf);
    window.console.log(dateDeal);
    //获取当前年份
    let year = date.getFullYear();
    window.console.log(year);
    /*
    *获取当前月份
    *当前月份为什么需要+1,因为月份是1-12而对应的常量是0-11,所以需要+1
    */
    let month = date.getMonth() + 1;
    window.console.log(month);
    //获取当前日
    let day = date.getDate();
    window.console.log(day);
    //获取当前小时
    let hour = date.getHours();
    window.console.log(hour);
    //获取当前分钟
    let mins = date.getMinutes();
    window.console.log(mins);
    //获取当前秒钟
    let seconds = date.getSeconds();
    window.console.log(seconds);
    //获取当前毫秒钟
    let millSeconds = date.getMilliseconds();
    window.console.log(millSeconds);

    /*
    *当前时间点加上120分钟
    *第一步获取当前处于的分钟
    *第二步当前的分钟加上120分钟
    *第三步用setMinutes()方法
    */
    let timeInAll = date.getMinutes() + 120;
    let time = date.setMinutes(timeInAll);
    window.console.log(date.getHours());
    window.console.log(date.getMinutes());
    /*
    *当前时间点上加上10个小时
    */
    let hourInAll = date.getHours() + 10;
    let hourChange = date.setHours(hourInAll);
    window.console.log(date.getHours());
    /*
    *当前时间点加上12天
    */
    let dayInAll = date.getDate() + 12;
    let dayChange = date.setDate(dayInAll);
    window.console.log(date.getDate());
    /*
    *当前的月份增加12个月
    */
    let monthInAll = date.getMonth() + 13;
    let monthChange = date.setMonth(monthInAll);
    window.console.log(date.getMonth());
    window.console.log(date.getFullYear());
    /*
    *当前的年份增加100年
    */
    let yearInAll = date.getFullYear() + 100;
    let yearChange = date.setFullYear(yearInAll);
    window.console.log(date.getFullYear());
    //获取当前月份天数
    let actualDay = new Date(year, month, 0).getDate();
    window.console.log(actualDay);
    
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值