/** * 时间处理工具 * * @type * @author miaofc */ export default { /** * 日期对象转换为毫秒数 */ date2Long: function (date) { return date.getTime(); }, /** * 毫秒转换为日期对象 * @param dateVal number 日期的毫秒数 */ long2Date: function (dateVal) { return new Date(dateVal); }, /** * 将制定格式的时间字符串转换成long *
  • 0-yyyyMMdd
  • *
  • 1-yyyy-MM-dd
  • *
  • 2-HHmmss
  • *
  • 3-HH:mm:ss
  • *
  • 4-HHmmssSSS
  • *
  • 5-HH:mm:ss.SSS
  • *
  • 6-yyyyMMddHHmmss
  • *
  • 7-yyyy-MM-dd HH:mm:ss
  • *
  • 8-yyyyMMddHHmmssSSS
  • *
  • 9-yyyy-MM-dd HH:mm:ss.SSS
  • *
  • 10-yyyy/MM/dd HH:mm
  • *
  • 11-yyyy/MM/dd HH:mm:ss
  • * */ string2Long: function (str, format) { if (str == null || str == "") { return ""; } return this.date2Long(this.string2Date(str, format)); }, /** * 毫秒转换为日期对象 * * @param dateVal * */ long2String: function (dateVal, formatType) { if (dateVal == undefined || dateVal == null) { return '-' } return this.date2String(new Date(dateVal), formatType); }, /** * 将时间转化为相应字符串 * * @param {} * date 待格式化的Date对象 * @param {} * formatType 指定格式: * * @return string 格式化后的字符串 */ date2String: function (date, formatType) { if (undefined == date || null == date) { return '-'; } // 格式化字符串数组 var patterns = ["yyyyMMdd", "yyyy-MM-dd", "HHmmss", "HH:mm:ss", "HHmmssSSS", "HH:mm:ss.SSS", "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmssSSS", "yyyy-MM-dd HH:mm:ss.SSS", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd", "yyyy-MM", "HH:mm"]; return this.formatDate(date, patterns[formatType]); }, /** * 将字符串转化为相应时间 * * @param {} * dateStr 时间字符串 * @param {} * formatType 指定格式: * * @return 解析后的Date对象 */ string2Date:function (date,formatType) { let dateTime; switch (formatType) { case 0 : dateTime= moment(date).format("YYYYMMDD"); break; case 1 : dateTime= moment(date).format("YYYY-MM-DD"); break; case 2 : dateTime= moment(date).format("HHmmss"); break; case 3 : dateTime= moment(date).format("HH:mm:ss"); break; case 4 : dateTime= moment(date).format("HHmmssSSS"); break; case 5 : dateTime= moment(date).format("HH:mm:ss.SSS"); break; case 6 : dateTime= moment(date).format("YYYY-MM-DDHHmmss"); break; case 7 : dateTime= moment(date).format("YYYY-MM-DD HH:mm:ss"); break; case 8 : dateTime= moment(date).format("yyyyMMddHHmmssSSS"); break; case 9 : dateTime= moment(date).format("yyyy-MM-dd HH:mm:ss.SSS"); break; case 10 ://yyyy/MM/dd HH:mm dateTime= moment(date).format("yyyy/MM/dd HH:mm"); break; case 11 ://yyyy/MM/dd HH:mm:ss dateTime= moment(date).format("yyyy/MM/dd HH:mm:ss"); break; } return dateTime; }, /** * 格式化时间 * * @param {} * date 待格式化的Date对象 * @param {} * pattern 格式化模式,可能包含下列字母 * * @return 格式化后的字符串 */ formatDate: function (date, pattern) { var dateStr = new String(pattern); // 格式化公元信息 dateStr = dateStr.replace("G", date.getFullYear() >= 0 ? "AD" : "BC"); // 格式化年份信息 var year = new String(date.getFullYear()); dateStr = dateStr.replace("yyyy", year); dateStr = dateStr.replace("yy", year.substring(year.length - 2, year.length)); // 格式化月份信息 var month = date.getMonth(); var monthFullNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; dateStr = dateStr.replace("MMMM", monthFullNames[month]); dateStr = dateStr.replace("MMM", monthShortNames[month]); dateStr = dateStr.replace("MM", month < 9 ? "0" + (month + 1) : month + 1); // 格式化月份中的日期 var day = date.getDate(); dateStr = dateStr.replace("dd", day < 10 ? "0" + day : day); // 格式化上下午 dateStr = dateStr.replace("a", date.getHours() < 12 ? "AM" : "PM"); // 格式化小时 var hour = date.getHours(); dateStr = dateStr.replace("HH", hour < 10 ? "0" + hour : hour); var khour = hour > 11 ? hour - 12 : hour; dateStr = dateStr.replace("KK", khour < 10 ? "0" + khour : khour); // 格式化分钟 var minute = date.getMinutes(); dateStr = dateStr.replace("mm", minute < 10 ? "0" + minute : minute); // 格式化秒钟 var second = date.getSeconds(); dateStr = dateStr.replace("ss", second < 10 ? "0" + second : second); // 格式化毫秒 var millisecond = date.getMilliseconds(); dateStr = dateStr.replace("SSS", millisecond < 10 ? "00" + millisecond : (millisecond < 100 ? "0" + millisecond : millisecond)); return dateStr; }, /** * 取得date在域field上偏移amount后的值 * * @param {} * date 原始时间 * @param {} * field 偏移域,可能的取值: * * @param {} * amount 偏移量 * @return 偏移后的时间 */ dateOffset: function (date, field, amount) { var mount = 0; switch (field) { // 以年为单位位移 case "year", "y": mount = 31536000 * 1000; // newDate.setFullYear(date.getFullYear()+amount); break; // 以月份为单位位移 case "month", "M": mount = 30 * 24 * 60 * 60 * 1000; // newDate.setMonth(date.getMonth()+amount); break; // 以天为单位位移 case "day", "d": mount = 24 * 60 * 60 * 1000; // newDate.setDate(date.getDate()+amount); break; // 以小时为单位位移 case "hour", "h": mount = 60 * 60 * 1000; // newDate.setHours(date.getHours()+amount); break; // 以分钟为单位位移 case "minute", "m": mount = 60 * 1000; // newDate.setMinutes(date.getMinutes()+amount); break; // 以秒为单位位移 case "second", "s": mount = 1000; // newDate.setSeconds(date.getSeconds+amount); break; // 以毫秒为单位位移 case "millisecond", "ms", "S": mount = 1; // newDate.setMilliseconds(date.getMilliseconds+amount); break; } var newDate = new Date(date.getTime() + Number(mount) * Number(amount)); return newDate; }, /** * 求两个日期间相差的毫秒数 * * @param {} * date1 时间1 * @param {} * date2 时间2 * @return 两个时间相差的毫秒数 */ getDiffMillis: function (date1, date2) { return date1.getTime() - date2.getTime(); }, /** * 求两个日期间相差的秒数 * * @param {} * date1 时间1 * @param {} * date2 时间2 * @return 两个时间相差的秒数 */ getDiffSeconds: function (date1, date2) { return Math.floor(this.getDiffMillis(date1, date2) / 1000); }, /** * 求两个日期间相差的分钟数目 * * @param {} * date1 时间1 * @param {} * date2 时间2 * @return 两个时间相差的分钟数 */ getDiffMinutes: function (date1, date2) { return Math.floor(this.getDiffSeconds(date1, date2) / 60); }, /** * 求两个日期间相差的小时数目 * * @param {} * date1 时间1 * @param {} * date2 时间2 * @return 两个时间相差的小时数 */ getDiffHours: function (date1, date2) { return Math.floor(this.getDiffMinutes(date1, date2) / 60); }, /** * 求两个日期间相差的天数 * * @param {} * date1 时间1 * @param {} * date2 时间2 * @return 两个时间相差的天数 */ getDiffDays: function (date1, date2) { return Math.floor(this.getDiffHours(date1, date2) / 24); }, /** * 求两个日期间相差的月数目。认为每个月均为30天。 * * @param {} * date1 时间1 * @param {} * date2 时间2 * @return 两个时间相差的月树 */ getDiffMonths: function (date1, date2) { return Math.floor(this.getDiffDays(date1, date2) / 30); }, /** * 求两个日期间相差的自然月数目 * * @param {} * date1 时间1 * @param {} * date2 时间2 * @return 两个时间相差的自然月数 */ getDiffNaturalMonth: function (date1, date2) { var month1 = date1.getMonth(); var month2 = date2.getMonth(); return this.getDiffYears(date1, date2) * 12 - (month2 - month1); }, /** * 求两个日期间相差的年数目 * * @param {} * date1 时间1 * @param {} * date2 时间2 * @return 两个时间相差的年数 */ getDiffYears: function (date1, date2) { return date1.getFullYear() - date2.getFullYear(); }, /** * 求日期为其所在月份的第几天 * * @param {} * day 目标日 * @return 目标日是第几天。天数从1开始 */ getOrinalOfDayInYear: function (day) { // 得到一年中的第一天 var firstDay = new Date(day.getFullYear, 0, 1); // 计算与第一天的差值 return this.getDiffDays(day, firstDay) + 1; }, /** * 求日期为其所在月份的第几天 * * @param {} * day 目标日 * @return 目标日是第几天。天数从1开始 */ getOrinalOfDayInMonth: function (day) { // 得到一个月中的第一天 var firstDay = new Date(day.getFullYear, day.getMonth(), 1); // 计算与第一天的差值 return this.getDiffDays(day, firstDay) + 1; }, /** * 求日期为其所在周的第几天 * * @param {} * day 目标日 * @return 目标日是第几天。天数从1开始 */ getOrinalOfDayInWeek: function (day) { return day.getDay() + 1; }, /** * 求某年共有多少天 * * @param {} * year 年份 * @return 该年份共有多少天 */ getNumberOfDaysInYear: function (year) { // 若该年份为闰年,返回366 if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)) { return 366; } // 普通年份返回365 return 365; }, /** * 求某月共有多少天 * * @param {} * year 年份 * @param {} * month 月份 * @return 该月内共有多少天 */ getNumberOfDaysInMonth: function (year, month) { // 得到该月份的第一天 var date1 = new Date(year, month - 1, 1); // 得到后一个月份的第一天 var date2 = ""; if (month != 11) { date2 = new Date(year, month, 1); } else { date2 = new Date(year - 1, 0, 1); } return this.getDiffDays(date1, date2); }, /** * 得到当前年份一共有多少天 * * @return 当前年份共有多少天 */ getNumberOfDaysInCurrentYear: function () { var date = new Date(); return this.getNumberOfDaysInYear(date.getYear()); }, /** * 得到当前月份一共有多少天 * * @return 当前月份共有多少天 */ getNumberOfDaysInCurrentMonth: function () { var date = new Date(); return this.getNumberOfDaysInMonth(date.getYear(), date.getMonth() + 1); }, // 金钱格式处理 moneyFormatter: function (value) { if (value == '0' || value == undefined || value == null || value === '') { return '0.00'; } else { return (value / 100).toFixed(2); } } };