iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >js获取最近一周一个月三个月时间的示例分析
  • 622
分享到

js获取最近一周一个月三个月时间的示例分析

2023-06-22 06:06:41 622人浏览 独家记忆
摘要

js获取最近一周一个月三个月时间的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。获取近一周时间var end = new D

js获取最近一周一个月三个月时间的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

获取近一周时间

var end = new Date();var year = end.getFullYear();var month = end.getMonth() + 1;//0-11表示1-12月var day = end.getDate();var dateObj = {};dateObj.end = year + '-' + month + '-' + day;if (day - 7 <= 0) {   //如果在当月7日之前    var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();    //1周前所在月的总天数    if (month - 1 <= 0) { //如果在当年的1月份      dateObj.start = (year - 1) + '-' + 12 + '-' + (31 - (7 - day));    } else {      dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (7 - day));    }} else {    dateObj.start = year + '-' + month + '-' + (day - 7);}console.log(JSON.stringify(dateObj))1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

获取近一个月时间

var end = new Date();var year = end.getFullYear();var month = end.getMonth() + 1;//0-11表示1-12月var day = end.getDate();var dateObj = {};dateObj.end = year + '-' + month + '-' + day;var endMonthDay = new Date(year, month, 0).getDate();    //当前月的总天数if(month - 1 <= 0){ //如果是1月,年数往前推一年<br>        dateObj.start = (year - 1) + '-' + 12 + '-' + day;}else{    var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();    if(startMonthDay < day){    //1个月前所在月的总天数小于现在的天日期        if(day < endMonthDay){        //当前天日期小于当前月总天数            dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day));        }else{            dateObj.start = year + '-' + (month - 1) + '-' + startMonthDay;        }    }else{        dateObj.start = year + '-' + (month - 1) + '-' + day;    }}console.log(JSON.stringify(dateObj))1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.

获取近三个月时间

var end = new Date();var year = end.getFullYear();var month = end.getMonth() + 1;//0-11表示1-12月var day = end.getDate();var dateObj = {};dateObj.end = year + '-' + month + '-' + day;var endMonthDay = new Date(year, month, 0).getDate();    //当前月的总天数if(month - 3 <= 0){ //如果是1、2、3月,年数往前推一年    var start3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate();    //3个月前所在月的总天数    if(start3MonthDay < day){    //3个月前所在月的总天数小于现在的天日期        dateObj.start = (year - 1) + '-' + (12 - (3 - month)) + '-' + start3MonthDay;    }else{        dateObj.start = (year - 1) + '-' + (12 - (3 - month)) + '-' + day;    }}else{    var start3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate();    //3个月前所在月的总天数    if(start3MonthDay < day){    //3个月前所在月的总天数小于现在的天日期        if(day < endMonthDay){        //当前天日期小于当前月总天数,2月份比较特殊的月份            dateObj.start = year + '-' + (month - 3) + '-' + (start3MonthDay - (endMonthDay - day));        }else{            dateObj.start = year + '-' + (month - 3) + '-' + start3MonthDay;        }    }else{        dateObj.start = year + '-' + (month - 3) + '-' + day;    }}console.log(JSON.stringify(dateObj))

New Date()与setDate()参数

相信网上已经有很多关于日期的文章了,这里只是我自己再工作中遇到的问题然后加以总结

new Date()

new Date() 一共有六种形式,五种带参数的一种不带参数的;

  1. new Date();自然不用多说,默认获取的是当前日期。

  2. new Date("month2 dd,yyyy hh:mm:ss"); 注意:参数是字符形式

  3. new Date("month2 dd,yyyy"); 注意:参数是字符形式

  4. new Date(yyyy,month3,dd,hh,mm,ss); 注意:参数不是字符

  5. new Date(yyyy,month3,dd); 注意:参数不是字符

  6. new Date(ms); 

参数说明:

month2:用英文,表示月份名称;从January到December ;

dd:表示日期,1-31

yyyy:表示四位表示的年份

hh:mm:ss:表示时间,时(0-23)-分(0-59)-秒(0-59)

month3:是Number型的月份;从0-11;即1月到12月

ms:从1970年1月1日之间相差的毫秒数

特别提醒:有些是字符形式有些不是

关于js获取最近一周一个月三个月时间的示例分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网精选频道了解更多相关知识。

--结束END--

本文标题: js获取最近一周一个月三个月时间的示例分析

本文链接: https://www.lsjlt.com/news/302979.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作