前端js常用的时间函数整理

摘要:有很多用到了时间函数,报错直接获取本月的开始日期,结束如期。本周的开始时间,结束时间等。这里简单的记录一下,方便下次引用。时间格式大家可以自行修改

这次的项目中,有很多用到了时间函数,报错直接获取本月的开始日期,结束如期。本周的开始时间,结束时间等。这里简单的记录一下,方便下次引用。时间格式大家可以自行修改,例子中都是格式化成为了2019-07-01 15:55:00这样的格式。


1.最常用的一个,就是对JS原生new Date()的扩展,可以格式成为自己想要的格式。(以下的函数都是需要调用这个的,所以必须要引入这个)

/**
* 对Date的扩展,将 Date 转化为指定格式的String
* 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
* 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
* 例子: 
* (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
* (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
*/
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


2.获取这个月,或者上个月,或者下个月的开始日期和结束日期,大家简单的修改,就能获取到本月的开始时间和结束时间

//获取当月或者上个月数据的开始日期和结束日期 get_date(0):当月的开始和结束 get_date(-1):上个月的开始和结束日期
        function get_date(addMonth){ 
            var now_date = new Date();
            var month = now_date.getMonth();
            var year = now_date.getFullYear();
            var this_yue_begin = new Date(new Date(year,month,1)).Format("yyyy-MM-dd");
            var this_yue_end = "";
            var set_yue_begin = "";
            var set_yue_end = "";
            if(addMonth){
                if((month+addMonth)>=12){
                    set_yue_begin = new Date(new Date(year+1,(month+addMonth-12),1)).Format("yyyy-MM-dd");
                }else if((month+addMonth)<=0){
                    set_yue_begin = new Date(new Date(year-1,11,1)).Format("yyyy-MM-dd");
                }else{
                    set_yue_begin = new Date(new Date(year,month+addMonth,1)).Format("yyyy-MM-dd");
                }
                
                if(month+addMonth==11){
                    set_yue_end = new Date(new Date(year+1,0,1).getTime()-1000).Format("yyyy-MM-dd");
                }else{
                    set_yue_end = new Date(new Date(year,month+addMonth+1,1).getTime()-1000).Format("yyyy-MM-dd");
                }
                return set_yue_begin+" 00:00:00 - "+set_yue_end+" 23:59:59";
            }else{
                if(month==11){
                    this_yue_end = new Date(new Date(year+1,0,1).getTime()-1000).Format("yyyy-MM-dd");
                }else{
                    this_yue_end = new Date(new Date(year,month+1,1).getTime()-1000).Format("yyyy-MM-dd");
                }
                return this_yue_begin +" 00:00:00 - "+this_yue_end+" 23:59:59";
            }
        }


3.获取本周的开始时间和结束时间

//获取本周的开始和结束时间,里面也是传0是获取本周的时间,传-1是获取上一周的
        function getWeekStartAndEnd(AddWeekCount) { 
            //起止日期数组   
            var startStop = new Array(); 
            //一天的毫秒数   
            var millisecond = 1000 * 60 * 60 * 24; 
            //获取当前时间   
            var currentDate = new Date();
            //相对于当前日期AddWeekCount个周的日期
            currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount));
            //返回date是一周中的某一天
            var week = currentDate.getDay(); 
            //返回date是一个月中的某一天   
            var month = currentDate.getDate();
            //减去的天数   
            var minusDay = week != 0 ? week - 1 : 6; 
            //获得当前周的第一天   
            var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay)); 
            //获得当前周的最后一天
            var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
            //添加至数组   
            startStop.push(getDateStr3(currentWeekFirstDay)); 
            startStop.push(getDateStr3(currentWeekLastDay)); 
        
            return getDateStr3(currentWeekFirstDay)+" 00:00:00 - "+getDateStr3(currentWeekLastDay)+" 23:59:59"; 
        }
        function getDateStr3(date) {
            var year = "";
            var month = "";
            var day = "";
            var now = date;
            year = ""+now.getFullYear();
            if((now.getMonth()+1)<10){
                month = "0"+(now.getMonth()+1);
            }else{
                month = ""+(now.getMonth()+1);
            }
            if((now.getDate())<10){
                day = "0"+(now.getDate());
            }else{
                day = ""+(now.getDate());
            }
            return year+"-"+month+"-"+day;
        }


4.获取最近三十天的时间

function get_30_date(){
        var now_date = new Date().Format("yyyy-MM-dd hh:mm:ss");
         var ago_30 = new Date(new Date().getTime()-30*24*60*60*1000).Format("yyyy-MM-dd hh:mm:ss");
       return ago_30+" - "+now_date;
 }


本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_3973