zoukankan      html  css  js  c++  java
  • JS获取本周、本季度、本月、上月的开始日期、结束日期

    1. /** 
    2.  * 获取本周、本季度、本月、上月的开始日期、结束日期 
    3.  */  
    4. var now = new Date();                    //当前日期     
    5. var nowDayOfWeek = now.getDay();         //今天本周的第几天     
    6. var nowDay = now.getDate();              //当前日     
    7. var nowMonth = now.getMonth();           //当前月     
    8. var nowYear = now.getYear();             //当前年     
    9. nowYear += (nowYear < 2000) ? 1900 : 0;  //    
    10.   
    11. var lastMonthDate = new Date();  //上月日期  
    12. lastMonthDate.setDate(1);  
    13. lastMonthDate.setMonth(lastMonthDate.getMonth()-1);  
    14. var lastYear = lastMonthDate.getYear();  
    15. var lastMonth = lastMonthDate.getMonth();  
    16.     
    17. //格式化日期:yyyy-MM-dd     
    18. function formatDate(date) {      
    19.     var myyear = date.getFullYear();     
    20.     var mymonth = date.getMonth()+1;     
    21.     var myweekday = date.getDate();      
    22.          
    23.     if(mymonth < 10){     
    24.         mymonth = "0" + mymonth;     
    25.     }      
    26.     if(myweekday < 10){     
    27.         myweekday = "0" + myweekday;     
    28.     }     
    29.     return (myyear+"-"+mymonth + "-" + myweekday);      
    30. }      
    31.     
    32. //获得某月的天数     
    33. function getMonthDays(myMonth){     
    34.     var monthStartDate = new Date(nowYear, myMonth, 1);      
    35.     var monthEndDate = new Date(nowYear, myMonth + 1, 1);      
    36.     var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);      
    37.     return   days;      
    38. }     
    39.     
    40. //获得本季度的开始月份     
    41. function getQuarterStartMonth(){     
    42.     var quarterStartMonth = 0;     
    43.     if(nowMonth<3){     
    44.        quarterStartMonth = 0;     
    45.     }     
    46.     if(2<nowMonth && nowMonth<6){     
    47.        quarterStartMonth = 3;     
    48.     }     
    49.     if(5<nowMonth && nowMonth<9){     
    50.        quarterStartMonth = 6;     
    51.     }     
    52.     if(nowMonth>8){     
    53.        quarterStartMonth = 9;     
    54.     }     
    55.     return quarterStartMonth;     
    56. }     
    57.     
    58. //获得本周的开始日期     
    59. function getWeekStartDate() {      
    60.     var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);      
    61.     return formatDate(weekStartDate);     
    62. }      
    63.     
    64. //获得本周的结束日期     
    65. function getWeekEndDate() {      
    66.     var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));      
    67.     return formatDate(weekEndDate);     
    68. }      
    69.     
    70. //获得本月的开始日期     
    71. function getMonthStartDate(){     
    72.     var monthStartDate = new Date(nowYear, nowMonth, 1);      
    73.     return formatDate(monthStartDate);     
    74. }     
    75.     
    76. //获得本月的结束日期     
    77. function getMonthEndDate(){     
    78.     var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));      
    79.     return formatDate(monthEndDate);     
    80. }     
    81.   
    82. //获得上月开始时间  
    83. function getLastMonthStartDate(){  
    84.     var lastMonthStartDate = new Date(nowYear, lastMonth, 1);  
    85.     return formatDate(lastMonthStartDate);    
    86. }  
    87.   
    88. //获得上月结束时间  
    89. function getLastMonthEndDate(){  
    90.     var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));  
    91.     return formatDate(lastMonthEndDate);    
    92. }  
    93.     
    94. //获得本季度的开始日期     
    95. function getQuarterStartDate(){     
    96.          
    97.     var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);      
    98.     return formatDate(quarterStartDate);     
    99. }     
    100.     
    101. //或的本季度的结束日期     
    102. function getQuarterEndDate(){     
    103.     var quarterEndMonth = getQuarterStartMonth() + 2;     
    104.     var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));      
    105.     return formatDate(quarterStartDate);     
    106. }  
  • 相关阅读:
    Excel中输入身份证后3位变成0,怎么办?
    Css中如何使英文和拼音变成全大写、全小写和首字母大写?
    css中 font常用的样式属性
    Css的向左浮动、先右浮动、绝对定位、相对定位的简单使用
    如何解决Css属性text-overflow:ellipsis 不起作用(文本溢出显示省略号)
    mysql 基本语法学习1(数据库、数据表、数据列的操作)
    sql server中如何将两个字段数据合并成一个字段显示(字段与字段添加特殊符号)
    Linq to Entity 求最大小值Max/Min返回null的处理方法
    C#匿名对象在其它方法体内怎么取到相应的值(不想建立对应的类并转化的情况下)?
    【转发】JQuery中操作Css样式的方法
  • 原文地址:https://www.cnblogs.com/telwanggs/p/4584001.html
Copyright © 2011-2022 走看看