常用工具处理类
来源:互联网 发布:淘宝正品代购可信吗 编辑:程序博客网 时间:2024/06/16 13:34
//常用日期处理帮助类var DateUtil = function () { //标准化时间格式字段串为"YYYY-MM-DD HH:mm:ss" function StandardFormate(fmt) { if (fmt) { fmt = fmt.trim(); var fmt_res = ""; for (i = 0; i < fmt.length; i++) { var curVal = fmt.charAt(i); if (curVal == "y" || curVal == "Y") { fmt_res += "Y"; } else if (curVal == "M" || curVal == "m") { if (fmt.indexOf(" ") > 0) { if (i < fmt.indexOf(" ")) { fmt_res += "M"; } else if (i > fmt.indexOf(" ")) { fmt_res += "m"; } } else { if (fmt.indexOf(":") < 0) { fmt_res += "M"; } else { fmt_res += "m"; } } } else if (curVal == "d" || curVal == "D") { fmt_res += "D"; } else if (curVal == "h" || curVal == "H") { fmt_res += "H"; } else if (curVal == "S" || curVal == "s") { fmt_res += "s"; } else if (curVal == "-" || curVal == "/") { fmt_res += "-"; } else if (curVal == " " || curVal == " ") { fmt_res += " "; } else if (curVal == ":") { fmt_res += ":"; } } return fmt_res; } else { return "YYYY-MM-DD HH:mm:ss"; } } // 重写toString方法,将时间转换为Y-m-d H:i:s格式 Date.prototype.toString = function (fmt) { if (fmt) { return moment(this).format(StandardFormate(fmt)); } else { return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate() + " " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds(); } } //格式化时间字符串 Date.prototype.Format = function (fmt) { return moment(this).format(StandardFormate(fmt)); } //在当前时间上添加年数 Date.prototype.addYear = function (years) { var cyear = this.getFullYear(); cyear += years; this.setYear(cyear); return this; } //在当前时间上添加天数 Date.prototype.addDay = function (days) { var cd = this.getDate(); cd += days; this.setDate(cd); return this; } //在当前时间上添加月数 Date.prototype.addMonth = function (months) { var cm = this.getMonth(); cm += months; this.setMonth(cm); return this; } //去除字符串空格 String.prototype.trim = function () { return this.replace(/(^\s*)|(\s*$)/g, ""); } //字符串生成日期指定格式日期:"2016-05-05 16:23:20".dateFormat("YYYY-MM-DD HH:mm:ss") String.prototype.dateFormat = function (format) { return moment(this.toString()).format(StandardFormate(format)); } this.getCurrentDate = function () { return new Date(); }; this.getCurrentWeek = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //返回date是一周中的某一天 var week = currentDate.getDay(); //返回date是一个月中的某一天 var month = currentDate.getDate(); //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //减去的天数 var minusDay = week != 0 ? week - 1 : 6; //alert(minusDay); //本周 周一 var monday = new Date(currentDate.getTime() - (minusDay * millisecond)); //本周 周日 var sunday = new Date(monday.getTime() + (6 * millisecond)); //添加本周时间 startStop.push(monday); //本周起始时间 //添加本周最后一天时间 startStop.push(sunday); //本周终止时间 //返回 return startStop; }; this.getCurrentMonth = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前月份0-11 var currentMonth = currentDate.getMonth(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //求出本月第一天 var firstDay = new Date(currentYear, currentMonth, 1); //当为12月的时候年份需要加1 //月份需要更新为0 也就是下一年的第一个月 if (currentMonth == 11) { currentYear++; currentMonth = 0; //就为 } else { //否则只是月份增加,以便求的下一月的第一天 currentMonth++; } //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //下月的第一天 var nextMonthDayOne = new Date(currentYear, currentMonth, 1); //求出上月的最后一天 var lastDay = new Date(nextMonthDayOne.getTime() - millisecond); //添加至数组中返回 startStop.push(firstDay); startStop.push(lastDay); //返回 return startStop; }; this.getQuarterSeasonStartMonth = function (month) { var quarterMonthStart = 0; var spring = 0; //春 var summer = 3; //夏 var fall = 6; //秋 var winter = 9; //冬 //月份从0-11 if (month < 3) { return spring; } if (month < 6) { return summer; } if (month < 9) { return fall; } return winter; }; this.getMonthDays = function (year, month) { //本月第一天 1-31 var relativeDate = new Date(year, month, 1); //获得当前月份0-11 var relativeMonth = relativeDate.getMonth(); //获得当前年份4位年 var relativeYear = relativeDate.getFullYear(); //当为12月的时候年份需要加1 //月份需要更新为0 也就是下一年的第一个月 if (relativeMonth == 11) { relativeYear++; relativeMonth = 0; } else { //否则只是月份增加,以便求的下一月的第一天 relativeMonth++; } //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //下月的第一天 var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1); //返回得到上月的最后一天,也就是本月总天数 return new Date(nextMonthDayOne.getTime() - millisecond).getDate(); }; this.getCurrentSeason = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前月份0-11 var currentMonth = currentDate.getMonth(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //获得本季度开始月份 var quarterSeasonStartMonth = this.getQuarterSeasonStartMonth(currentMonth); //获得本季度结束月份 var quarterSeasonEndMonth = quarterSeasonStartMonth + 2; //获得本季度开始的日期 var quarterSeasonStartDate = new Date(currentYear, quarterSeasonStartMonth, 1); //获得本季度结束的日期 var quarterSeasonEndDate = new Date(currentYear, quarterSeasonEndMonth, this.getMonthDays(currentYear, quarterSeasonEndMonth)); //加入数组返回 startStop.push(quarterSeasonStartDate); startStop.push(quarterSeasonEndDate); //返回 return startStop; }; this.getCurrentYear = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //本年第一天 var currentYearFirstDate = new Date(currentYear, 0, 1); //本年最后一天 var currentYearLastDate = new Date(currentYear, 11, 31); //添加至数组 startStop.push(currentYearFirstDate); startStop.push(currentYearLastDate); //返回 return startStop; }; this.getPriorMonthFirstDay = function (year, month) { //年份为0代表,是本年的第一月,所以不能减 if (month == 0) { month = 11; //月份为上年的最后月份 year--; //年份减1 return new Date(year, month, 1); } //否则,只减去月份 month--; return new Date(year, month, 1); ; }; this.getPreviousYear = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); currentYear--; var priorYearFirstDay = new Date(currentYear, 0, 1); var priorYearLastDay = new Date(currentYear, 11, 1); //添加至数组 startStop.push(priorYearFirstDay); startStop.push(priorYearLastDay); return startStop; }; //上期日 this.getPriorDay = function (date) { var _date = createDate(date); if (_date) { var yesterday_milliseconds = _date.getTime() - 1000 * 60 * 60 * 24; var yesterday = new Date(); yesterday.setTime(yesterday_milliseconds); var strYear = yesterday.getFullYear(); var strDay = yesterday.getDate(); var strMonth = yesterday.getMonth() + 1; var strHour = yesterday.getHours(); var strMinutes = yesterday.getMinutes(); var strSec = yesterday.getSeconds(); if (strMonth < 10) { strMonth = "0" + strMonth; } datastr = strYear + "-" + strMonth + "-" + strDay; if (strHour) { datastr += " " + strHour } if (strHour && strMinutes) { datastr += ":" + strMinutes } if (strHour && strMinutes && strSec) { datastr += ":" + strSec } return datastr; } else { return ""; } } //上期周 this.getPreviousWeek = function (date) { var _date = (typeof date == "object" && date.getFullYear ? date : createDate(date)); //起止日期数组 var startStop = new Array(); //返回date是一周中的某一天 var week = _date.getDay(); //返回date是一个月中的某一天 var month = _date.getDate(); //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //减去的天数 var minusDay = week != 0 ? week - 1 : 6; //获得当前周的第一天 var currentWeekDayOne = new Date(_date.getTime() - (millisecond * minusDay)); //上周最后一天即本周开始的前一天 var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond); //上周的第一天 var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6)); //添加至数组 startStop.push(priorWeekFirstDay); startStop.push(priorWeekLastDay); return startStop; }; //上期月 this.getPreviousMonth = function (date) { var _date = (typeof date == "object" && date.getFullYear ? date : createDate(date)); //起止日期数组 var startStop = new Array(); //获得当前月份0-11 var currentMonth = _date.getMonth(); //获得当前年份4位年 var currentYear = _date.getFullYear(); //获得上一个月的第一天 var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth); //获得上一月的最后一天 var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth())); //添加至数组 startStop.push(priorMonthFirstDay); startStop.push(priorMonthLastDay); //返回 return startStop; }; //上期季 this.getPreviousSeason = function (date) { var _date = (typeof date == "object" && date.getFullYear ? date : createDate(date)); //起止日期数组 var startStop = new Array(); //获得当前月份0-11 var currentMonth = _date.getMonth(); //获得当前年份4位年 var currentYear = _date.getFullYear(); //上季度的第一天 var priorSeasonFirstDay = this.getPriorSeasonFirstDay(currentYear, currentMonth); //上季度的最后一天 var priorSeasonLastDay = new Date(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2, this.getMonthDays(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2)); //添加至数组 startStop.push(priorSeasonFirstDay); startStop.push(priorSeasonLastDay); return startStop; }; //判断闰年,是闰年则返回true否则返回false this.isLeapYear = function (year) { var pYear = year; if (!isNaN(parseInt(pYear))) { if ((pYear % 4 == 0 && pYear % 100 != 0) || (pYear % 100 == 0 && pYear % 400 == 0)) { return true; } else { return false; } } else { return false; } } //返回标准格式日期的各个组成部分 this.getTimeStr = function (date) { //日期自动补零 function ModifyForm(time) { if (time < 10) { return "0" + time; } return time; }; if (date) { var resStr = ""; var sDay = date.getDate(), sMonth = date.getMonth() + 1, sYear = date.getFullYear(), sHour = date.getHours(), sMinutes = date.getMinutes(), sSec = date.getSeconds(); if (sYear) { resStr += (sYear + ""); } if (sYear && sMonth) { resStr += ("-" + ModifyForm(sMonth)); } if (sYear && sMonth && sDay) { resStr += ("-" + ModifyForm(sDay)); } if (sYear && sMonth && sDay && sHour) { resStr += (" " + ModifyForm(sHour)); } if (sYear && sMonth && sDay && sHour && sMinutes) { resStr += (":" + ModifyForm(sMinutes)); } if (sYear && sMonth && sDay && sHour && sMinutes && sSec) { resStr += (":" + ModifyForm(sSec)); } return resStr; } else { return ""; } } //增加天数 date为时间格式字符串,days为增加天数(为负数时则为减) this.addOrSubDays = function (date, days) { if (!isNaN(days)) { var a = (typeof date == "object" && date.getFullYear ? date : createDate(date)); a = a.valueOf() a = a + days * 24 * 60 * 60 * 1000 a = new Date(a) return getTimeStr(a); } } //获取指定月份的最后一天 this.getMonthLastDay = function (date) { date = (typeof date == "object" && date.getFullYear ? (getTimeStr(date)) : date); var y = date.substr(0, 4); var m = date.substr(5, 2); var nextMonthDay = new Date(y, m, 1); var nowMonthLastDay = new Date(nextMonthDay.getTime() - 24 * 60 * 60 * 1000); return getTimeStr(nowMonthLastDay); }; //相差天数 this.dateDiff = function (date1, date2) { //字符串转成Time(dateDiff)所需方法 function stringToTime(string) { var f = string.split(' ', 2); var d = (f[0] ? f[0] : '').split('-', 3); var t = (f[1] ? f[1] : '').split(':', 3); return (new Date( parseInt(d[0], 10) || null, (parseInt(d[1], 10) || 1) - 1, parseInt(d[2], 10) || null, parseInt(t[0], 10) || null, parseInt(t[1], 10) || null, parseInt(t[2], 10) || null )).getTime(); } var type1 = typeof date1, type2 = typeof date2; if (type1 == 'string') date1 = stringToTime(date1); else if (date1 && date1.getTime) date1 = date1.getTime(); if (type2 == 'string') date2 = stringToTime(date2); else if (date2 && date2.getTime) date2 = date2.getTime(); return (date2 - date1) / (1000 * 60 * 60 * 24); //结果是天数 } //返回一个date日期 this.createDate = function (date) { // var datePart = null; //采用标准的new Date对象的方式 if (typeof date == "object" && date.getFullYear) { return date; } else { date = date.trim(); date = date.replace("/", "-"); var datePart = []; if (date.indexOf(" ") > -1 && date.indexOf(":") > -1) { var TimeArr_1 = date.split(" "); //遍历处理日期部分和时间部分 for (var i = 0; i < TimeArr_1.length; i++) { if (TimeArr_1[i].indexOf("-") > -1) { var a = TimeArr_1[i].split("-"); for (var j = 0; j < a.length; j++) { if (j == 1) { datePart.push(parseInt(a[j]) - 1); } else { datePart.push(a[j]); } } } else if (TimeArr_1[i].indexOf(":") > -1) { var b = TimeArr_1[i].split(":"); for (var h = 0; h < b.length; h++) { datePart.push(b[h]); } } } } else { var TimeArr_2 = date.split("-"); for (var k = 0; k < TimeArr_2.length; k++) { if (k == 1) { datePart.push(parseInt(TimeArr_2[k]) - 1); } else { datePart.push(TimeArr_2[k]); } } } } //根据日期数组的长度实例化时间变量 if (datePart && datePart.length && datePart.length > 0) { switch (datePart.length) { case 6: return new Date(datePart[0], datePart[1], datePart[2], datePart[3], datePart[4], datePart[5]); break; case 5: return new Date(datePart[0], datePart[1], datePart[2], datePart[3], datePart[4]); break; case 4: return new Date(datePart[0], datePart[1], datePart[2], datePart[3]); break; case 3: return new Date(datePart[0], datePart[1], datePart[2]); break; case 2: return new Date(datePart[0], datePart[1]); break; case 1: return new Date(datePart[0]); break; } } } return { getCurrentDate: function () { return getCurrentDate(); }, getCurrentWeek: function () { return getCurrentWeek(); }, getCurrentMonth: function () { return getCurrentMonth(); }, getQuarterSeasonStartMonth: function (month) { return getQuarterSeasonStartMonth(month); }, getMonthDays: function (year, month) { return getMonthDays(year, month); }, getCurrentSeason: function () { return getCurrentSeason(); }, getCurrentYear: function () { return getCurrentYear(); }, getPriorMonthFirstDay: function (year, month) { return getPriorMonthFirstDay(year, month); }, getPreviousMonth: function (date) { return getPreviousMonth(date); }, getPreviousWeek: function (date) { return getPreviousWeek(date); }, getPriorSeasonFirstDay: function (year, month) { return getPriorSeasonFirstDay(year, month); }, getPreviousSeason: function (date) { return getPreviousSeason(date); }, getPreviousYear: function (date) { return getPreviousYear(date); }, getTimeStr: function (date) { return getTimeStr(date); }, getPriorDay: function (date) { return getPriorDay(date); }, isLeapYear: function (year) { return isLeapYear(year); }, addOrSubDays: function (date, days) { return addOrSubDays(date, days); }, getMonthLastDay: function (date) { return getMonthLastDay(date); }, dateDiff: function (date1, date2) { return dateDiff(date1, date2); }, createDate: function (date) { return createDate(date); } };} ();
阅读全文
0 0
- 常用工具处理类
- Java常用工具类五 -- 文件处理相关
- Android开发常用工具类之图像处理
- Android开发常用工具类之图像处理
- 处理字节的常用工具类方法
- 倾斜摄影处理常用工具
- Shell文本处理常用工具
- 十三、java 常用工具类使用UUid、日期处理、MD5加密
- Java回炉重塑之常用工具类(字符串处理)
- Android常用工具类之Log的特别处理
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 常用工具类
- 制作centos base image for docker
- GitHub添加SSH-KEY
- centos7一键安装mysql
- C++存在重定义的解决办法
- 迭代器失效的场景
- 常用工具处理类
- 【POJ 2572】Seek the Name, Seek the Fame
- oracle数据中删除数据时提示“记录被另一个用户锁住” 解决方法: 1、查看数据库锁,诊断锁的来源及类型: select object_id
- leetcode Reverse Integer & Palindrome Number
- c++调用lua文件
- angular2 集成 Editor.md
- bind
- 性能测试常见指标
- 15.4. Abstract Base Classes