0
点赞
收藏
分享

微信扫一扫

27js学习第十天Date对象

查拉图斯特拉你和他 2022-04-21 阅读 104
javascript

     // 01JavaScript的Date对象
    
    // 使用new 关键字 Date() 构造函数
    // var oDate=new Date(参数)

    // 1.获取当前日期 时间
    var oDate1=new Date();//无参数时  获取当前日期时间
    console.log(oDate1);

    // 2.自定义时间  
    // new Date(year,month,day,hour,minute,second,millisecond)
    // 参数为纯数字时 表示为毫秒数  返回距离1970年1月1日0点的毫秒数
    var oDate2=new Date(100000);//
    console.log(oDate2);//Thu Jan 01 1970 08:01:40 GMT+0800 (中国标准时间)  计算机中将1970年1月1日0时0份0秒作为时间纪元。

    // 将年月日作为参数传进去
    var oDate3=new Date(2022,1,1);
    console.log(oDate3);
    // 时分秒
    var oDate4=new Date(2022,3,29,18,0,0);
    console.log(oDate4)

    // 时间格式 字符串作为参数
    var oDate5=new Date("2022-4-29,18:00:00");
    var oDate6=new Date("2022/4/29,18:00:00");
    console.log(oDate5);
    console.log(oDate6);
 

 // 02JavaScript的Date对象的属性
    
    // 使用new 关键字 Date() 构造函数
    // var oDate=new Date(参数)

    // 1.获取当前日期 时间
    var oDate1=new Date();//无参数时  获取当前日期时间
    console.log(oDate1);
    console.log(oDate1.name);
    console.log(Date.prototype);
    // constructor 返回对创建此对象的 Date 函数的引用 
    // prototype 向对象中添加属性和方法  原型
    Date.prototype.name="日期时间对象"
    console.log(new Date().name)

    // 2.自定义时间  
    // new Date(year,month,day,hour,minute,second,millisecond)
    // 参数为纯数字时 表示为毫秒数  返回距离1970年1月1日0点的毫秒数

    var oDate2=new Date(100000);//
    console.log(oDate2);//Thu Jan 01 1970 08:01:40 GMT+0800 (中国标准时间)  计算机中将1970年1月1日0时0份0秒作为时间纪元。

    // 将年月日作为参数传进去
    var oDate3=new Date(2022,1,1);
    console.log(oDate3);
    // 时分秒
    var oDate4=new Date(2022,3,29,18,0,0);
    console.log(oDate4)

    // 时间格式 字符串作为参数
    var oDate5=new Date("2022-4-29,18:00:00");
    var oDate6=new Date("2022/4/29,18:00:00");
    console.log(oDate5);
    console.log(oDate6);
 

// 03JavaScript的Date对象的方法
    
    // 1.获取当前日期 时间
    var oDate1=new Date();
    console.log(oDate1);
    console.log(Date.prototype);

    // getFullYear() 从 Date 对象以四位数字返回年份
    console.log(oDate1.getFullYear());//2022
    // getMonth() 从 Date 对象返回月份 (0 ~ 11)。1月-12月
    console.log(oDate1.getMonth());//3
    // getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)
    console.log(oDate1.getDate());//21
    // getDay()    从 Date 对象返回一周中的某一天 (0 ~ 6)。周日-周六
    console.log(oDate1.getDay());//4
    // getHours() 返回 Date 对象的小时 (0 ~ 23)
    console.log(oDate1.getHours());//17
    // getMinutes()    返回 Date 对象的分钟 (0 ~ 59)
    console.log(oDate1.getMinutes());
    // getSeconds()    返回 Date 对象的秒数 (0 ~ 59)
    console.log(oDate1.getSeconds());//43
    // getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)
    console.log(oDate1.getMilliseconds());

    // getTime() 返回 1970 年 1 月 1 日至今的毫秒数
    console.log(oDate1.getTime());
    var oDate2=new Date(100000);
    console.log(oDate2.getTime());
    


    // 格林威治时间  
    // getUTCDate()    根据世界时从 Date 对象返回月中的一天 (1 ~ 31)
    // getUTCDay()    根据世界时从 Date 对象返回周中的一天 (0 ~ 6)
    // getUTCFullYear()    根据世界时从 Date 对象返回四位数的年份
    // getUTCHours()    根据世界时返回 Date 对象的小时 (0 ~ 23)
    console.log(oDate1.getUTCHours());//9
    // getUTCMilliseconds()    根据世界时返回 Date 对象的毫秒(0 ~ 999)
    // getUTCMinutes()    根据世界时返回 Date 对象的分钟 (0 ~ 59)
    // getUTCMonth()    根据世界时从 Date 对象返回月份 (0 ~ 11)
    // getUTCSeconds()    根据世界时返回 Date 对象的秒钟 (0 ~ 59)
    // getYear()    已废弃。 请使用 getFullYear() 方法代替。


    // 设置
    // setFullYear()    设置 Date 对象中的年份(四位数字)
    // setMonth()    设置 Date 对象中月份 (0 ~ 11)。    
    // setDate()    设置 Date 对象中月的某一天 (1 ~ 31)
    // setHours()    设置 Date 对象中的小时 (0 ~ 23)
    // setMinutes()    设置 Date 对象中的分钟 (0 ~ 59)
    // setSeconds()    设置 Date 对象中的秒钟 (0 ~ 59)。    
    // setMilliseconds()    设置 Date 对象中的毫秒 (0 ~ 999)

    // 设置世界时间  格林威治时间
    // setUTCFullYear()    根据世界时设置 Date 对象中的年份(四位数字)
    // setUTCDate()    根据世界时设置 Date 对象中月份的一天 (1 ~ 31)
    // setUTCHours()    根据世界时设置 Date 对象中的小时 (0 ~ 23)
    // setUTCMilliseconds()    根据世界时设置 Date 对象中的毫秒 (0 ~ 999)
    // setUTCMinutes()    根据世界时设置 Date 对象中的分钟 (0 ~ 59)
    // setUTCMonth()    根据世界时设置 Date 对象中的月份 (0 ~ 11)
    // setUTCSeconds()    setUTCSeconds() 方法用于根据世界时 (UTC) 


    // getYear() 已废弃。 请使用 getFullYear() 方法代替。
    // console.log(oDate1.getYear());


 

举报

相关推荐

JS学习第十天

第十天学习

24js学习第十天Number对象

python(第十天)

学习SSM的第十天

【JavaSE 第十天】

web第十天

0 条评论