27js学习第十天Date对象

     // 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  


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javascript完全自学手册 目 录 第1篇 JavaScript基础篇 第1章 JavaScript简介 1 1.1 JavaScript概述 1 1.1.1 什么是JavaScript 1 1.1.2 JavaScript的基本特点 2 1.1.3 常用的Web开发语言 3 1.2 JavaScript的应用 4 1.2.1 客户端应用 5 1.2.2 服务器端应用 5 1.3 在Web页面中使用JavaScript 5 1.3.1 HTML的基本结构 5 1.3.2 在HTML中嵌入JavaScript 7 1.3.3 链接JavaScript文件 9 1.4 编写JavaScript的工具 11 1.4.1 使用纯文本编辑器 11 1.4.2 使用专业化脚本编辑工具 13 1.4.3 使用Microsoft脚本编辑器 15 第2章 JavaScript编程基础 19 2.1 基础语法 19 2.1.1 数据类型 19 2.1.2 变量和常量 22 2.1.3 表达式 24 2.1.4 运算符 24 2.2 流程控制 27 2.2.1 条件语句 27 2.2.2 循环语句 30 2.2.3 其他语句 35 2.3 使用对话框 38 2.3.1 警告对话框 38 2.3.2 确认对话框 40 2.3.3 提示对话框 40 第2篇 JavaScript内置对象篇 第3章 JavaScript内置对象 43 3.1 JavaScript对象概述 43 3.1.1 对象的概念 43 3.1.2 使用JavaScript对象 基础知识 44 3.2 Array对象 46 3.2.1 创建Array对象 46 3.2.2 Array对象属性 47 3.2.3 Array对象方法 48 3.3 String对象 51 3.3.1 创建String对象 51 3.3.2 String对象属性 51 3.3.3 String对象方法 53 3.4 Math对象 56 3.5 Date对象 62 3.5.1 Date对象方法 62 3.5.2 使用Date对象 63 3.6 自定义对象 67 第4章 JavaScript常用对象 73 4.1 Document对象 73 4.1.1 Document对象概述 73 4.1.2 使用Document对象 75 4.2 Form对象及其元素 79 4.2.1 Form对象概述 79 4.2.2 表单元素 80 4.2.3 表单元素属性和事件 82 4.2.4 表单验证 85 4.3 Anchor与Link对象 89 4.3.1 Anchor对象 89 4.3.2 Link对象 90 4.4 Image对象 92 4.4.1 图像翻转 92 4.4.2 图像载入 92 第5章 JavaScript其他常用 窗口对象 96 5.1 Window对象 96 5.1.1 常用属性和方法 97 5.1.2 多窗口控制 100 5.2 Screen对象 103 5.3 Navigator对象 105 5.4 Location对象 107 5.4.1 常用属性和方法 107 5.4.2 Location对象的应用实例 109 5.5 History对象 112 5.5.1 常用属性和方法 112 5.5.2 History对象的应用实例 113 5.6 Frame对象 115 5.6.1 Frame对象概述 115 5.6.2 常用属性 116 5.6.3 Frame对象的应用实例 117 第6章 DOM对象 122 6.1 DOM概述 122 6.1.1 DOM简介 122 6.1.2 DOM与HTML文档 124 6.2 DOM对象 125 6.2.1 DOM基本接口 125 6.2.2 DOM基本对象 128 6.3 使用DOM 128 6.3.1 DOM基础 129 6.3.2 Node和NodeList接口 131 6.3.3 Element接口 134 6.3.4 Text接口和Attr接口 136 6.4 操作HTML文档 138 6.4.1 访问元素 138 6.4.2 添加节点 140 6.4.3 删除节点 142 6.4.4 对属性进行操作 146 第3篇 JavaScript高级编程篇 第7章 正则表达式 151 7.1 正则表达式及其作用 151 7.2 正则表达式参考语法 152 7.2.1 限定符 154 7.2.2 选择匹配符 157 7.2.3 分组组合
你可以使用 JavaScript 中的 Date 对象和一些日期操作方法来获取上月第一天至上月最后一天的日期。 以下是获取上月第一天和最后一天日期的代码示例: ```javascript // 获取当前日期 const today = new Date(); // 获取上月的年份和月份 const lastMonthYear = today.getFullYear(); const lastMonth = today.getMonth() - 1; if (lastMonth < 0) { lastMonthYear--; lastMonth = 11; } // 获取上月的第一天和最后一天日期 const firstDayOfLastMonth = new Date(lastMonthYear, lastMonth, 1); const lastDayOfLastMonth = new Date(lastMonthYear, lastMonth + 1, 0); // 格式化日期字符串 const firstDayOfLastMonthStr = firstDayOfLastMonth.toISOString().substring(0, 10); const lastDayOfLastMonthStr = lastDayOfLastMonth.toISOString().substring(0, 10); console.log('上月第一天日期为:', firstDayOfLastMonthStr); console.log('上月最后一天日期为:', lastDayOfLastMonthStr); ``` 以上代码中,我们首先获取当前日期,然后计算上月的年份和月份。接着使用 `new Date()` 构造函数和年、月、日参数来创建上月第一天和最后一天的日期对象。最后使用 `toISOString()` 方法将日期对象转换为 ISO 格式的日期字符串,并截取其中的年月日部分。 需要注意的是,JavaScript 中的日期操作存在时区问题,可能会导致日期计算错误。因此建议在进行日期计算时,将所有日期都转换为 UTC 时间进行计算,再转换回本地时间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值