JavaScript---day09

1.时间对象Date

        月份单词

January:一月     February:二月      March:三月       April:四月 
May:五月      June:六月    July:七月    August:八月   
September:九月    October:十月    November:十一月      December:十二月

1.1 创建时间对象

  • 创建当前时间对象

<script>
        // 1.创建当前这一时刻的时间对象
        var oDate =   new Date();
        console.log(oDate);// Thu Sep 01 2022 09:57:06 GMT+0800 (中国标准时间)

        // 2.创建每秒的时间
        setInterval(function(){
            var a = new Date();
            // console.log(a,"现在时刻");
        },1000) 
    </script>
  • 创建自定义时间

  //a. 自定义的时间是一个字符串
        var sDate =  new Date("2023,1,1,12:00:00");
        var sDate =  new Date("2023 1 1 12:00:00");
        var sDate =  new Date("2023/1/1 12:00:00");
        var sDate =  new Date("2023-1-1 12:00:00");
        console.log(sDate);//Sun Jan 01 2023 12:00:00 GMT+0800 (中国标准时间)

        //b. 时间分开接受参数  创建2023年10月1日  12:00:00的时间
        // 问题  想创建10月   但是结果出来的是11月  
        // 分析原因: 月份是从0开始的   0-1月   10-11月
        var aDate = new Date(2023,9,1,12,0,0)  //想创建10月  就给9
        console.log(aDate);// Sun Oct 01 2023 12:00:00 GMT+0800 (中国标准时间)

        var bDate =  new Date(2023,9,1);
        console.log(bDate);// Sun Oct 01 2023 00:00:00 GMT+0800 (中国标准时间)

1.2 获取时间

  • 获取特定格式的时间

 var  oDate = new Date();
        // 年月日  时分秒  日期
        console.log(oDate);//Thu Sep 01 2022 10:39:02 GMT+0800 (中国标准时间)

        // 1.获取特定格式的时间    年  月  日
        console.log(oDate.toDateString());//Thu Sep 01 2022
        console.log(oDate.toLocaleDateString());//2022/9/1

        // 2.获取特定格式的时间    时  分  秒
        console.log(oDate.toTimeString());//10:41:30 GMT+0800 (中国标准时间)
        console.log(oDate.toLocaleTimeString());// 10:42:02
  • 获取单个时间

 // 3.获取单个时间
        // 年
        console.log(oDate.getFullYear());//2022\
        // 月  月份是从0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值