27js学习第十天Date对象

本文详细介绍了JavaScript Date对象的创建、获取当前日期、自定义时间、属性及方法,包括getFullYear(), getMonth(), getDate()等,并展示了如何转换和格式化日期字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     // 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());

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


    // 转换字符串
    // toString() 把Date对象转换为字符串。
    console.log(oDate1.toString());
    // toDateString() 把Date对象的日期部分转换为字符串。
    console.log(oDate1.toDateString());//Fri Apr 22 2022
    // toTimeString() 把Date对象的时间部分转换为字符串。
    console.log(oDate1.toTimeString());//10:48:19 GMT+0800 (中国标准时间)
    
    // toLocaleString() 根据本地时间格式,把Date对象转换为字符串。
    console.log(oDate1.toLocaleString());//2022/4/22 上午10:50:31
    // toLocaleDateString() 根据本地时间格式,把Date对象的日期部分转换为字符串。
    console.log(oDate1.toLocaleDateString());
    // toLocaleTimeString() 根据本地时间格式,把Date对象的时间部分转换为字符串。
    console.log(oDate1.toLocaleTimeString());

    // toUTCString() 根据世界时间,把Date对象转换为字符串
    console.log(oDate1.toUTCString());//Fri, 22 Apr 2022 02:53:01 GMT
    // toISOString() 使用ISO标准返回字符串的日期格式。
    console.log(oDate1.toISOString());//2022-04-22T02:54:43.953Z

    // toJSON() 以 JSON 数据格式返回日期字符串。
    console.log(oDate1.toJSON());//2022-04-22T02:57:02.910Z
    
    // valueOf() 返回Date对象的原始值
    console.log(oDate1.valueOf());//1650596386824
    console.log(new Date(1650596386824));

    // UTC() 根据世界时间返回1970年1月1日到指定日期的毫秒数
    console.log(Date.UTC(1970,0,2));//86400000
    // parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数
    console.log(Date.parse("1970,1,2"));//57600000  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值