jq获取当前日期前一个月(javascript获取当前系统时间)

//方法1: // 格式化时间-精确到分钟 //提示:如果得到的是number类型必须转换为string类型才可以解析,否则会报错 //获取到日期 var vehicleNextInsDate = jsonObj.vehicleNextInsDate; //将日期转换为字符串 var stringDate = vehicleNextInsDate.toString(); //日期处理函数 function ChangeDateFormat(jsondate) { jsondate = jsondate.replace(“/Date(“, “”).replace(“)/”, “”); if (jsondate.indexOf(” “) > 0) { jsondate = jsondate.substring(0, jsondate.indexOf(” “)); } else if (jsondate.indexOf(“-“) > 0) { jsondate = jsondate.substring(0, jsondate.indexOf(“-“)); } var date = new Date(parseInt(jsondate, 10)); var month = date.getMonth() 1 < 10 ? “0” (date.getMonth() 1) : date.getMonth() 1; var currentDate = date.getDate() < 10 ? “0” date.getDate() : date.getDate(); return date.getFullYear() “-” month “-” currentDate ” ” ” ” date.getHours() “:” date.getMinutes(); }; //调用日期函数 ChangeDateFormat(stringDate); //——————————————————————————————————————– //方法2: //格式化时间-精确到秒 function getNowFormatDate(millsTime) { var day = new Date(millsTime); var Year = 0; var Month = 0; var Day = 0; var Hour = 0; var Minute = 0; var Second = 0; var CurrentDate = “”; Year = day.getFullYear(); //支持IE和火狐浏览器. Month = day.getMonth() 1; Day = day.getDate(); Hour = day.getHours(); Minute = day.getMinutes(); Second = day.getSeconds(); CurrentDate = Year ‘/’; if (Month >= 10) { CurrentDate = Month ‘/’; } else { CurrentDate = “0” Month ‘/’; } if (Day >= 10) { CurrentDate = Day ‘ ‘; } else { CurrentDate = “0” Day ‘ ‘; } if (Hour >= 10) { CurrentDate = Hour ‘:’; } else { CurrentDate = ‘0’ Hour ‘:’; } if (Minute >= 10) { CurrentDate = Minute ‘:’; } else { CurrentDate = ‘0’ Minute ‘:’; } if (Second >= 10) { CurrentDate = Second; } else { CurrentDate = ‘0’ Second; } return CurrentDate; } //调用 var vehicleFactoryDate = jsonObj.vehicleFactoryDate; getNowFormatDate(vehicleFactoryDate) / //– — — — — — — — — — — — — — — — — — — //时间戳转换成时间不带时分秒“-” function getLocalTime(nS) { var Y, M, D, W, H, I, S; function fillZero(v) { if (v < 10) { v = ‘0’ v; } return v; } var d = new Date(parseInt(nS / 1000) * 1000); var Week = [‘星期天’, ‘星期一’, ‘星期二’, ‘星期三’, ‘星期四’, ‘星期五’, ‘星期六’]; Y = d.getFullYear(); M = fillZero(d.getMonth() 1); D = fillZero(d.getDate()); W = Week[d.getDay()]; H = fillZero(d.getHours()); I = fillZero(d.getMinutes()); S = fillZero(d.getSeconds()); var localTime = Y ‘-‘ M ‘-‘ D; return localTime; } ///————————————————- //获取当前时间带时分秒 function getTimehms(ns) { var date = new Date(); var seperator1 = “-“; var seperator2 = “:”; var month = date.getMonth() 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = “0” month; } if (strDate >= 0 && strDate <= 9) { strDate = “0” strDate; } var currentdate = date.getFullYear() seperator1 month seperator1 strDate ” ” date.getHours() seperator2 date.getMinutes() seperator2 date.getSeconds(); return currentdate; } ///—————- //获取当前时间不带时分秒 function newgetNowFormatDate() { var date = new Date(); var seperator1 = “-“; var seperator2 = “:”; var month = date.getMonth() 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = “0” month; } if (strDate >= 0 && strDate <= 9) { strDate = “0” strDate; } var currentdate = date.getFullYear() seperator1 month seperator1 strDate; return currentdate; } //————————— // 计算两个日期相差多少月(time1<time2) this.monthDiffer = function(time1, time2) { var time; var oDate1 = new Date(time1); var oDate2 = new Date(time2); time = (oDate2.getFullYear() – oDate1.getFullYear()) * 12 (oDate2.getMonth() – oDate1.getMonth() 1) return time } //——————————- this.dayDiffer = function(s1, s2) { s1 = new Date(s1); s2 = new Date(s2); var days = s2.getTime() – s1.getTime(); var time = Math.floor(days / (24 * 60 * 60 * 1000)); return time } //———————– // 获取当月天数(格式为yyyy-mm-dd) this.getCountDays = function(m) { var d = new Date(m); var curMonthDays = new Date(d.getFullYear(), (d.getMonth() 1), 0).getDate(); return curMonthDays; } //———————– //js中获取时间new date() 的用法 //获取时间: //1 var myDate = new Date(); //获取系统当前时间 //获取特定格式的时间: /*1*/ myDate.getYear(); //获取当前年份(2位) /*2*/ myDate.getFullYear(); //获取完整的年份(4位,1970-????) /*3*/ myDate.getMonth(); //获取当前月份(0-11,0代表1月) /*4*/ myDate.getDate(); //获取当前日(1-31) /*5*/ myDate.getDay(); //获取当前星期X(0-6,0代表星期天) /*6*/ myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) /*7*/ myDate.getHours(); //获取当前小时数(0-23) /*8*/ myDate.getMinutes(); //获取当前分钟数(0-59) /*9*/ myDate.getSeconds(); //获取当前秒数(0-59) /*10*/ myDate.getMilliseconds(); //获取当前毫秒数(0-999) /*11*/ myDate.toLocaleDateString(); //获取当前日期 /*12*/ var mytime = myDate.toLocaleTimeString(); //获取当前时间 /*13*/ myDate.toLocaleString(); //获取日期与时间 // JS获取当前时间戳的方法 // JavaScript 获取当前时间戳: // 第一种方法: /*1*/ var timestamp = Date.parse(new Date()); // 得到的结果: 1280977330000 注意: 这里得到的结果将后三位( 毫秒) 转换成了000显示, 使用时可能会出现问题。 例如动态添加页面元素id的时候, 不建议使用。 // 第二种方法: /*1*/ var timestamp = (new Date()).valueOf(); // 结果: 1280977330748 // 第三种方法: var timestamp = new Date().getTime(); // 结果: 1280977330748 // js中单独调用new Date(), 例如document.write(new Date()); // 显示的结果是: Mar 31 10: 10: 43 UTC 0800 2012 这种格式的时间 // 但是用new Date() 参与计算会自动转换为从1970 .1 .1 开始的毫秒数。 / //– — — — — — — — — — — — — — — — — — — // 格式化日期, 将日期转化为字符串类型, 兼容所有浏览器 // 例如: 2018 – 08 – 08 转化为20180808 // var a = ‘2018 – 08 – 08’; // a = a.replace(/-/g, ”); //——————————————– //获取当天时间的零点 new Date(new Date().toLocaleDateString()).getTime() //—————————————————– /** *获取当前时间 *format=1精确到天 *format=2精确到分 */ function getCurrentDate(format) { var now = new Date(); var year = now.getFullYear(); //得到年份 var month = now.getMonth(); //得到月份 var date = now.getDate(); //得到日期 var day = now.getDay(); //得到周几 var hour = now.getHours(); //得到小时 var minu = now.getMinutes(); //得到分钟 var sec = now.getSeconds(); //得到秒 month = month 1; if (month < 10) month = “0” month; if (date < 10) date = “0” date; if (hour < 10) hour = “0” hour; if (minu < 10) minu = “0” minu; if (sec < 10) sec = “0” sec; var time = “”; //精确到天 if (format == 1) { time = year “-” month “-” date; } //精确到分 else if (format == 2) { time = year “-” month “-” date ” ” hour “:” minu “:” sec; } return time; } getCurrentDate(2); //——————— // 如何获取当前日期的前n天, 后n天的日期, 不带时分秒 function getDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate() AddDayCount); //获取AddDayCount天后的日期 var y = dd.getFullYear(); var m = dd.getMonth() 1; //获取当前月份的日期 var d = dd.getDate(); return y ‘-‘ (m < 10 ? ‘0’ m : m) ‘-‘ d; } console.log(‘前天:’, getDateStr(-2)); // 前天: 2018-09-11 console.log(‘昨天:’, getDateStr(-1)); // 昨天: 2018-09-12 console.log(‘今天:’, getDateStr(0)); // 今天: 2018-09-13 console.log(‘明天:’, getDateStr(1)); // 明天: 2018-09-14 console.log(‘后天:’, getDateStr(2)); // 后天: 2018-09-15 //——————— //获取当前时间前N天的日期,带时分秒,使用方法:GetDateStr(-7); function GetDateStr(AddDayCount) { var dd = new Date(); var dd2 = new Date(); dd.setDate(dd.getDate() AddDayCount); //获取AddDayCount天后的日期 var y = dd.getFullYear(); var M = (dd.getMonth() 1) < 10 ? “0” (dd.getMonth() 1) : (dd.getMonth() 1); //获取当前月份的日期,不足10补0 var d = dd.getDate() < 10 ? “0” dd.getDate() : dd.getDate(); //获取当前几号,不足10补0 var h = dd2.getHours() < 10 ? “0” dd2.getHours() : dd2.getHours(); var m = dd2.getMinutes() < 10 ? “0” dd2.getMinutes() : dd2.getMinutes(); var s = dd2.getSeconds() < 10 ? “0” dd2.getSeconds() : dd2.getSeconds(); return y “-” M “-” d ” ” h “:” m “:” s; } //—————————————————————— // Js计算时间差( 天、 小时、 分钟、 秒) function GetDateDiff(starttime, endtime, type) { if (starttime == null || endtime == null) { return “”; } var ed = endtime; var sd = starttime; if (type == 1) { ed = ed “:00”; } else if (type == 2) { sd = sd “:00”; } //此处是解决浏览器兼容性问题,Firefox只能认识2015/06/05格式 var sd2 = sd.replace(/-/g, “/”); var ed2 = ed.replace(/-/g, “/”); var startTime = new Date(sd2); var endTime = new Date(ed2); var result = “”; var date3 = endTime.getTime() – startTime.getTime(); //时间差的毫秒数 //计算出相差天数 var days = Math.floor(date3 / (24 * 3600 * 1000)); result = days > 0 ? days “天” : “0天”; //计算出小时数 var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数 var hours = Math.floor(leave1 / (3600 * 1000)); result = hours > 0 ? hours “小时” : “0小时”; //计算相差分钟数 var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数 var minutes = Math.floor(leave2 / (60 * 1000)); result = minutes > 0 ? minutes “分钟” : “0分钟”; //计算相差秒数 //var leave3=leave2%(60*1000); //计算分钟数后剩余的毫秒数 //var seconds=Math.round(leave3/1000); // //result = seconds > 0 ? seconds “秒” : “”; return result == “” ? “” : result; }; var a = GetDateDiff(‘2019-02-15 01:03:04’, ‘2019-03-15 02:05:08’); console.log(a); // 推荐使用:JS获取本周、本季度、本月、上月的开始日期、结束日期-方法1—————————————————————————————– var now = new Date(); //当前日期 var nowDayOfWeek = now.getDay(); //今天本周的第几天 var nowDay = now.getDate(); //当前日 var nowMonth = now.getMonth(); //当前月 var nowYear = now.getYear(); //当前年 nowYear = (nowYear < 2000) ? 1900 : 0; // var lastMonthDate = new Date(); //上月日期 lastMonthDate.setDate(1); lastMonthDate.setMonth(lastMonthDate.getMonth() – 1); var lastYear = lastMonthDate.getYear(); var lastMonth = lastMonthDate.getMonth(); //格式化日期:yyyy-MM-dd function formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.getMonth() 1; var myweekday = date.getDate(); if (mymonth < 10) { mymonth = “0” mymonth; } if (myweekday < 10) { myweekday = “0” myweekday; } return (myyear “-” mymonth “-” myweekday); } //获得某月的天数 function getMonthDays(myMonth) { var monthStartDate = new Date(nowYear, myMonth, 1); var monthEndDate = new Date(nowYear, myMonth 1, 1); var days = (monthEndDate – monthStartDate) / (1000 * 60 * 60 * 24); return days; } //获得本季度的开始月份 function getQuarterStartMonth() { var quarterStartMonth = 0; if (nowMonth < 3) { quarterStartMonth = 0; } if (2 < nowMonth && nowMonth < 6) { quarterStartMonth = 3; } if (5 < nowMonth && nowMonth < 9) { quarterStartMonth = 6; } if (nowMonth > 8) { quarterStartMonth = 9; } return quarterStartMonth; } //获得本周的开始日期 function getWeekStartDate() { var weekStartDate = new Date(nowYear, nowMonth, nowDay – nowDayOfWeek); return formatDate(weekStartDate); } //获得本周的结束日期 function getWeekEndDate() { var weekEndDate = new Date(nowYear, nowMonth, nowDay (6 – nowDayOfWeek)); return formatDate(weekEndDate); } //获得本月的开始日期 function getMonthStartDate() { var monthStartDate = new Date(nowYear, nowMonth, 1); return formatDate(monthStartDate); } //获得本月的结束日期 function getMonthEndDate() { var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth)); return formatDate(monthEndDate); } //获得上月开始时间 function getLastMonthStartDate() { var lastMonthStartDate = new Date(nowYear, lastMonth, 1); return formatDate(lastMonthStartDate); } //获得上月结束时间 function getLastMonthEndDate() { var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth)); return formatDate(lastMonthEndDate); } //获得本季度的开始日期 function getQuarterStartDate() { var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1); return formatDate(quarterStartDate); } //或的本季度的结束日期 function getQuarterEndDate() { var quarterEndMonth = getQuarterStartMonth() 2; var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth)); return formatDate(quarterStartDate); } //获取本周、本季度、本月、上月的开始日期、结束日期-方法1-end——————————————– //获取本周、本季度、本月、上月的开始日期、结束日期-方法2——————————————– var dateRangeUtil = (function() { /*** * 获得当前时间 */ this.getCurrentDate = function() { return new Date(‘2015′, ’07’, ‘-1’); }; //格局化日期:yyyy-MM-dd this.formatDate = function(date) { var myyear = date.getFullYear(); var mymonth = date.getMonth() 1; var myweekday = date.getDate(); if (mymonth < 10) { mymonth = “0” mymonth; } if (myweekday < 10) { myweekday = “0” myweekday; } return (myyear “-” mymonth “-” myweekday); } /*** * 获得本周起止时间 */ 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(this.formatDate(monday)); //本周起始时间 //添加本周最后一天时间 startStop.push(this.formatDate(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; }; /** * 得到本季度开始的月份 * @param month 需要计算的月份 ***/ 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; }; /** * 获得该月的天数 * @param year年份 * @param month月份 * */ 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; }; /** * 返回上一个月的第一天Date类型 * @param year 年 * @param month 月 **/ 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.getPreviousMonth = function() { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前月份0-11 var currentMonth = currentDate.getMonth(); //获得当前年份4位年 var currentYear = currentDate.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.getPreviousWeek = 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; //获得当前周的第一天 var currentWeekDayOne = new Date(currentDate.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; }; /** * 得到上季度的起始日期 * year 这个年应该是运算后得到的当前本季度的年份 * month 这个应该是运算后得到的当前季度的开始月份 * */ this.getPriorSeasonFirstDay = function(year, month) { var quarterMonthStart = 0; var spring = 0; //春 var summer = 3; //夏 var fall = 6; //秋 var winter = 9; //冬 //月份从0-11 switch (month) { //季度的其实月份 case spring: //如果是第一季度则应该到去年的冬季 year–; month = winter; break; case summer: month = spring; break; case fall: month = summer; break; case winter: month = fall; break; }; return new Date(year, month, 1); }; /** * 得到上季度的起止日期 * **/ this.getPreviousSeason = function() { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前月份0-11 var currentMonth = currentDate.getMonth(); //获得当前年份4位年 var currentYear = currentDate.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; }; /** * 得到去年的起止日期 * **/ 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; }; return this; })(); $(function() { console.log(dateRangeUtil.getCurrentWeek()); $(“#startDate”).html(dateRangeUtil.getCurrentWeek()); }) //获取本周、本季度、本月、上月的开始日期、结束日期-方法2-end——————————————–

发表评论

登录后才能评论