JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;...

学习目标

1.掌握创建日期对象的方法

2.掌握date对象中获取日期时间的方法

3.掌握date对象中设置日期时间的方法

如何创建一个日期对象

语法:new Date();

功能:创建一个日期时间对象

返回值:比传参的情况下,返回当前的日期时间对象。

说明:如果想根据特定的日期和时间创建日期对象,必需传入表示该日期的:

毫秒数或者是一组用逗号隔开的表示年月日时分秒的参数。

<script>
       // 创建一个日期时间对象
       var weeks=["日","一","二","三","四","五","六"],
           today=new Date(),
           year=today.getFullYear(),
           month=today.getMonth()+1,
           date=today.getDate(),
           week=today.getDay(),
           hours=today.getHours(),
           minutes=today.getMinutes(),
           seconds=today.getSeconds(),
           times=today.getTime(),
           time=year+'年'+month+'月'+date+'日'+hours+'时'+minutes+'分'+seconds+'秒 星期'+weeks[week];
       console.log("现在是:"+time);
       console.log(times);
    </script>

获取年月日时分秒及星期的方法

1.getFullYear():返回4位数的年份

2.getMonth():返回日期中的月份,返回值为0-11

3.getDate():返回月份中的天数

4.getDay():返回星期,返回值为0-6

5.getHours():返回小时

6.getMinutes():返回分

7.getSeconds():返回秒

8.getTime():返回表示日期的毫秒数

<script>
        // 创建一个日期时间对象
        var today=new Date();
        //today.setFullYear(2017);
        //today.setMonth(15);
        //console.log(today.getFullYear());
        //  50天之后是星期
        // 第一种做法
        //today.setDate(today.getDate()+50);
        //console.log(today.getDay());
        // 第二种做法
        var weeks=["日","一","二","三","四","五","六"];
        var year=today.getFullYear();
        var month=today.getMonth();
        var day=today.getDate();
        // 创建了一个目标日期对象,年,月,日,时,分,秒
        var temp=new Date(year+1,month,day);
        console.log("50天后的今天是:"+temp.getFullYear()+'-'+(temp.getMonth()+1)+'-'+temp.getDate()+'-'+'星期'+weeks[temp.getDay()]);
    </script>

设置年月日时分秒及星期的方法

1.setFullYear():返回4位数的年份

2.setMonth():返回日期中的月份,从0开始,0表示1月

3.setDate():设置日期

4.4.setDay():返回星期,从0开始,0表示星期日

5.setHours():返回小时

6.setMinutes():返回分

7.setSeconds():返回秒

8.setTime():以毫秒数设置日期,会改变整个日期

 <script>
        function addZero(num){
           if(num<10){
              return '0'+num;
           }else{
              return num;
           }
        }
        // 返回n天之后的日期时间对象
        function get_date(n){
           // 判断n,如果是未定义的,则返回当前日期,否则返回n天之后的日期
           n=typeof(n)==="undefined"?0:n;
           // 创建一个当前的日期时间对象
           var date=new Date(),
               times=date.getTime(),   // 到现在为止的毫秒数
               tempDate=new Date(),    // 未来的一个日期对象
               //times=date*1;  // 等价于getTime()隐式类型转换
               tempTimes=times+86400000*n,
               year,mon,day;
           // 将tempTimes设置为当前
           tempDate.setTime(tempTimes);
           year=tempDate.getFullYear();
           mon=addZero(tempDate.getMonth()+1);
           day=addZero(tempDate.getDate());
           return year+'-'+mon+'-'+day;
        }
        console.log(get_date(20));
    </script>

 

转载于:https://www.cnblogs.com/oybb/p/7631172.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值