首页 > 解决全网90%以上的日期格式转换、日期序列等骚操作问题

解决全网90%以上的日期格式转换、日期序列等骚操作问题

function getYearMonthList(startDate, endDate) {//返回月份的数组 如 ['2021/07','2021/08']var arr = [];var s = new Date(startDate).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).split("/");var e = new Date(endDate).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).split("/");var min = new Date();var max = new Date();min.setFullYear(s[0], s[1] * 1 - 1, 1);//开始日期max.setFullYear(e[0], e[1] * 1 - 1, 1);//结束日期var curr = min;while (curr <= max) {var month = curr.getMonth();arr.push(curr.getFullYear() + "/" + (month + 1).toString().padStart(2, "0"));curr.setMonth(month + 1);}return arr;
}console.log(getYearMonthList(new Date(2020, 1, 1), new Date(2021, 1, 1)));
//["2020/02", "2020/03", "2020/04", "2020/05", "2020/06", "2020/07", "2020/08", "2020/09", "2020/10", "2020/11", "2020/12", "2021/01", "2021/02"]
function getYearMonthDayList(startDate, endDate) { //返回 日期的数组 如 ['2021/07/10','2021/07/11']var arr = [];var s = new Date(startDate).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).split("/");var e = new Date(endDate).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).split("/");var min = new Date();var max = new Date();min.setUTCFullYear(s[0], s[1] - 1, s[2]);max.setUTCFullYear(e[0], e[1] - 1, e[2]);var unixDb = min.getTime() - 24 * 60 * 60 * 1000;var unixDe = max.getTime() - 24 * 60 * 60 * 1000;for (var date = unixDb; date <= unixDe; date += 24 * 60 * 60 * 1000) {arr.push(new Date(date).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}));}return arr;
}console.log(getYearMonthDayList(new Date(2021, 7, 1), new Date(2021, 7, 11)));
//["2021/08/01", "2021/08/02", "2021/08/03", "2021/08/04", "2021/08/05", "2021/08/06", "2021/08/07", "2021/08/08", "2021/08/09", "2021/08/10", "2021/08/11"]
//计算两个日期之间相差几个月
function getDisMonths(startDate, endDate) {startDate = new Date(startDate).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).split("/");endDate = new Date(endDate).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).split("/");var year1 = parseInt(startDate[0]),month1 = parseInt(startDate[1]),year2 = parseInt(endDate[0]),month2 = parseInt(endDate[1]),months = Math.abs(year2 - year1) * 12 + Math.abs(month2 - month1);return months;
}console.log(getDisMonths(new Date(2020, 5), new Date(2021, 7)));//14
// 当前日期转换为:yyyy/MM/dd HH:mm:ss(24小时制)________________________
new Date().toLocaleString("zh-Hans-CN", {dateStyle: "short", timeStyle: "medium", hour12: false});// 当前日期转换为:yyyy/MM/dd 上午/下午HH:mm:ss(12小时制)________________________
new Date().toLocaleString("zh-Hans-CN", {dateStyle: "short", timeStyle: "medium"});// 当前日期转换为:yyyy-MM-dd HH:mm:ss(24小时制 双位数)________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit", hour12: false, hour: "2-digit", minute: "2-digit", second: "2-digit"}).replace(///g, "-");// 当前日期转换为:yyyy-MM-dd________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).replace(///g, "-");// 当前日期转换为:HH:mm:ss(24小时制 双位数)________________________
new Date().toLocaleString("zh-Hans-CN", {hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false});// 当前日期转换为:HH:mm(24小时制 双位数)________________________
new Date().toLocaleString("zh-Hans-CN", {hour: "2-digit", minute: "2-digit", hour12: false});// 当前日期转换为:公元yyyy年MM月dd日星期D HH:mm:ss(24小时制)________________________
new Date().toLocaleString("zh-Hans-CN", {era:'short',year: "numeric", month: "short", day: "numeric", weekday: "long", hour: "numeric", minute: "numeric", second: "numeric", hour12: false});// 当前日期转换为:yyyy年MM月dd日星期D HH:mm:ss(24小时制)________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "short", day: "numeric", weekday: "long", hour: "numeric", minute: "numeric", second: "numeric", hour12: false});// 当前日期转换为:yyyy年MM月dd日周D HH:mm:ss(24小时制)________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "short", day: "numeric", weekday: "short", hour: "numeric", minute: "numeric", second: "numeric", hour12: false});// 当前日期转换为:yyyy年MM月dd日D HH:mm:ss(24小时制)________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "short", day: "numeric", weekday: "narrow", hour: "numeric", minute: "numeric", second: "numeric", hour12: false});// 当前日期转换为:yyyy年MM月dd日星期D HH:mm:ss(24小时制)________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", hour12: false});// 当前日期转换为:yyyy年MM月dd日星期D________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "short", day: "numeric", weekday: "long"});// 当前日期转换为:yyyy年MM月dd日周D________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "short", day: "numeric", weekday: "short"});// 当前日期转换为:yyyy年MM月dd日D________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "short", day: "numeric", weekday: "narrow"});// 当前日期转换为:yyyy年MM月dd日________________________
new Date().toLocaleString("zh-Hans-CN", {year: "numeric", month: "short", day: "numeric"});//部分笨拙的方法________________________
var yearMonthDay = new Date().getFullYear() + ("0" + (new Date().getMonth() + 1)).slice(-2) + ("0" + new Date().getDate()).slice(-2);//yyyyMMdd
var year_Month_Day = new Date().getFullYear() + "-" + ("0" + (new Date().getMonth() + 1)).slice(-2) + "-" + ("0" + new Date().getDate()).slice(-2);//yyyy-MM-dd//部分笨拙的方法(ES6)________________________
var yearMonthDay = new Date().getFullYear() + (new Date().getMonth() + 1).toString().padStart(2, 0) + (new Date().getDate()).toString().padStart(2, 0);//yyyyMMdd
var year_Month_Day = new Date().getFullYear() + "-" + (new Date().getMonth() + 1).toString().padStart(2, 0) + "-" + (new Date().getDate()).toString().padStart(2, 0);//yyyy-MM-dd//当月第一天、当月最后一天年月日(yyyy-MM-dd)________________________
new Date(new Date().getFullYear(),new Date().getMonth(),1).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).replace(///g, "-");
new Date(new Date().getFullYear(),new Date().getMonth()+1,0).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}).replace(///g, "-");/**日期方法大合集*/
var date = {//新方法(时:分:秒)HH_mm_ss(date = null) {return new Date(date).toLocaleString("zh-Hans-CN", { hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false });},//新方法(年-月-日)yyyy_MM_dd(date = null) {return new Date(date).toLocaleString("zh-Hans-CN", { year: "numeric", month: "2-digit", day: "2-digit" }).replace(///g, "-");},//新方法(年-月-日 时:分:秒)yyyy_MM_dd_HH_mm_ss(date = null) {return new Date(date).toLocaleString("zh-Hans-CN", { year: "numeric", month: "2-digit", day: "2-digit", hour12: false, hour: "2-digit", minute: "2-digit", second: "2-digit" }).replace(///g, "-");},yearMonthDay: function () {return new Date().getFullYear() + ("0" + (new Date().getMonth() + 1)).slice(-2) + ("0" + new Date().getDate()).slice(-2);}, year_Month_Day: function () {return new Date().getFullYear() + "-" + ("0" + (new Date().getMonth() + 1)).slice(-2) + "-" + ("0" + new Date().getDate()).slice(-2);}, yMd: function (dt, split) {dt || (dt = new Date());split || (split = "-");return dt.getFullYear() + split + ("0" + (dt.getMonth() + 1)).slice(-2) + split + ("0" + dt.getDate()).slice(-2);},/**判断是否逾期*/over: function (endDate, isEqual) {var d1 = new Date().getTime(), d2 = new Date(endDate).getTime();return isEqual ? d1 >= d2 : d1 > d2;},/**比较日期大小,前一个日期大于(isEqual=true时 比较大于等于)后一个日期时返回true*/compare: function (d1, d2, isEqual) {d1 = new Date(d1).getTime(), d2 = new Date(d2).getTime();return isEqual ? d1 >= d2 : d1 > d2;},/**获取指定日期之前/之后的某天*/pointDate: function (dt, n) {if (!n) return dt;var s = "/";if (dt.indexOf("-") > -1) {s = "-", dt = dt.replace(/-/g, "/");} else if (dt.indexOf(".") > -1) {s = ".", dt = dt.replace(/./g, "/");}var d = new Date(dt), lw = new Date(Number(d) + 1000 * 60 * 60 * 24 * Math.floor(n)), /*n天数*/ ly = lw.getFullYear(), lm = lw.getMonth() + 1, ld = lw.getDate(), sd = ly + s + (lm < 10 ? "0" + lm : lm) + s + (ld < 10 ? "0" + ld : ld);return sd;},/**获得当前日期之前之后任意天的日期*/anyDate: function (n) {var dt = new Date();dt.setDate(dt.getDate() + n);return this.yMd(dt);},/**获得当前日期之前之后任意天的日期+时间*/anyDateTime: function (n) {var dt = new Date();dt.setDate(dt.getDate() + n);return formatDateTime(dt);},/**获得任意天的日期时间戳:n为负数就是过去的天数,正数则为未来的天数*/anyDateTimeStamp: function (n) {return new Date(date.anyDate(n) + " 00:00:00").getTime();},/**获得本月的开始日期、结束日期*/monthStartOrEndDate: function (isStart) {var now = new Date(), m = now.getMonth(), y = now.getFullYear(), msd = new Date(y, m, Boolean(isStart) ? 1 : new Date(y, m + 1, 0).getDate());return date.yMd(msd);},/**获得本周的开始日期、结束日期*/weekStartOrEndDate: function (isStart) {var now = new Date(), d = now.getDay(), nd = now.getDate(), m = now.getMonth(), y = now.getFullYear(), wsd = new Date(y, m, nd + (Boolean(isStart) ? -d : 6 - d));return date.yMd(wsd);},/**计算指定日期加上多少天、加多少月、加多少年的日期*/add: function (type, number, date) {var d = date ? (date instanceof Date ? date : new Date(date)) : new Date();switch (type) {case "y":d.setFullYear(d.getFullYear() + number);return d;case "q":d.setMonth(d.getMonth() + number * 3);return d;case "m":d.setMonth(d.getMonth() + number);return d;case "w":d.setDate(d.getDate() + number * 7);return d;case "d":d.setDate(d.getDate() + number);return d;case "h":d.setHours(d.getHours() + number);return d;case "m":d.setMinutes(d.getMinutes() + number);return d;case "s":d.setSeconds(d.getSeconds() + number);return d;default:d.setDate(d.getDate() + number);return d;}/*/!* 加2天.*!/ alert(date.add("d ", 2).toLocaleString()) /!* 加2月.*!/ alert(date.add("m ", 2).toLocaleString()) /!* 加2年*!/ alert(date.add("y ", 2).toLocaleString());*/},format: function (date, fmt) {date = date instanceof Date ? date : new Date(date);var o = {"M+": date.getMonth() + 1,"d+": date.getDate(),"h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12,"H+": date.getHours(),"m+": date.getMinutes(),"s+": date.getSeconds(),"q+": Math.floor((date.getMonth() + 3) / 3),"S": date.getMilliseconds()};if (/(y+)/.test(fmt)) {fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));}if (/(E+)/.test(fmt)) {fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "星期" : "周") : "") + "日一二三四五六".charAt(date.getDay()));}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;},/**格式化日期:yyyy-MM-dd HH:mm:ss */formatDate: function (timeStamp) {return date.format(timeStamp ? (timeStamp instanceof Date ? timeStamp : new Date(timeStamp)) : new Date(), "yyyy-MM-dd");},/**格式化日期:yyyy-MM-dd HH:mm:ss */formatDateTime: function (timeStamp) {return date.format(timeStamp ? (timeStamp instanceof Date ? timeStamp : new Date(timeStamp)) : new Date(), "yyyy-MM-dd HH:mm:ss");},/**格式化日期:yyyy年MM月dd日 HH:mm:ss */formatToyyyyMMddHHmmssEE: function (timeStamp) {return date.format(timeStamp ? (timeStamp instanceof Date ? timeStamp : new Date(timeStamp)) : new Date(), "yyyy年MM月dd日 HH:mm:ss EE");}, getDay: function () {return "星期" + "日一二三四五六".charAt(new Date().getDay());},/**转换Date为24小时计时时间格式*/to24hours: function (date) {var now = date ? (date instanceof Date ? date : new Date(date)) : new Date(), now = now.toLocaleTimeString("zh-Hans-CN", {hour12: false}), now = now.substr(0, now.lastIndexOf(":"));return now;},/**将秒数量转换为时分秒字符串*/toHourMinuteSecond: function (second, data) {var t = "",s = Math.round(second),d = data.isDoubleDigits,//显示双位数hz = data.hideZero,//隐藏为0的时间单位hh = data.hideHour,//隐藏小时hm = data.hideMinute,//隐藏分钟hs = data.hideSecond;//隐藏秒钟if (s > 0) {var hour = Math.floor(s / 3600), min = Math.floor(s / 60) % 60, sec = s % 60;hh || (hz && !hour) || (d && hour < 10 && (t += "0"), t += hour + "时");hm || (hz && !min) || (d && min < 10 && (t += "0"), t += min + "分");hs || (hz && !sec) || (d && sec < 10 && (t += "0"), t += sec + "秒");}return t;//测试用例/*alert(toHourMinuteSecond(3661,{// isDoubleDigits:true,hideZero:true,// hideHour:true,// hideMinute:true,// hideSecond:true,}));*/},/**获取最近几个月的年月份*/getRecentSeveralMonth: function (n) {var date = new Date();var nearMonth = [];for (var i = 1; i <= n; i++) {date.setMonth(date.getMonth() - 1);nearMonth.unshift(date.getFullYear() + "/" + (date.getMonth() + 1));}return nearMonth;},/**把时间转换为分钟数*/hourMinuteToMinute: function (timeString) {timeString = timeString.replace(/:/g, ":").replace(/ | /g, "").replace(/::/g, ":").split(":");return parseInt(timeString[0] * 60) + parseInt(timeString[1]);},/**显示几分钟前刚刚发布文章*/timeAgo: function (timeStamp) {var minute = 1000 * 60, hour = minute * 60, day = hour * 24, week = day * 7, month = day * 30, now = new Date().getTime(), diffValue = now - timeStamp;var minC = diffValue / minute, hourC = diffValue / hour, dayC = diffValue / day, weekC = diffValue / week, monthC = diffValue / month, res;if (monthC > 3 && monthC < 12) {res = "半年前";} else if (monthC >= 1 && monthC <= 3) {res = parseInt(monthC) + "月前";} else if (weekC >= 1 && weekC < 4) {res = parseInt(weekC) + "周前";} else if (dayC >= 1 && dayC < 7) {res = parseInt(dayC) + "天前";} else if (hourC >= 1 && hourC < 24) {res = parseInt(hourC) + "小时前";} else if (minC >= 1 && minC < 60) {res = parseInt(minC) + "分钟前";} else if (diffValue >= 0 && diffValue <= minute) {res = "刚刚";} else {res = this.formatDateTime(timeStamp);}return res;}, between: function (startDate, endDate) {startDate = startDate instanceof Date ? startDate : new Date(startDate);endDate = endDate instanceof Date ? endDate : new Date(endDate);var today = new Date().getTime(), startDate = new Date(startDate).getTime(), endDate = new Date(endDate).getTime();return startDate < today && today < endDate;},/**计算两个日期相差天数*/getDayBetween: function (startDate, endDate) {return Math.floor(this.getMillisecondBetween(startDate, endDate)/ 86400000);},/**计算两个日期相差毫秒数*/getMillisecondBetween: function (startDate, endDate) {startDate = startDate instanceof Date ? startDate : new Date(startDate);endDate = endDate instanceof Date ? endDate : new Date(endDate);return Math.abs(Date.parse(endDate) - Date.parse(startDate));}
};//获取周一的日期
getMonday() {var day = new Date().getDay(); //获取时间的星期数var time = new Date().setDate(new Date().getDate() - (day ? day - 1 : 6)); //获取minus天前的日期return new Date(time).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}) + " 00:00:00";},
//获取周五的日期
getFriday() {var day = new Date().getDay(); //获取时间的星期数var time = new Date().setDate(new Date().getDate() + (day ? 5 - day : -2)); //获取minus天前的日期return new Date(time).toLocaleString("zh-Hans-CN", {year: "numeric", month: "2-digit", day: "2-digit"}) + " 00:00:00";},
//获取周日的日期
getSunday(index) {var day = new Date().getDay(); //获取时间的星期数var time = new Date().setDate(new Date().getDate() + (index * 7) + (day ? 7 - day : 0)); //获取minus天前的日期return new Date(time).toLocaleString("zh-Hans-CN", {year: "numeric",month: "2-digit",day: "2-digit"}).replace(///g, '-');},

更多相关:

  • 0.cal 2019  #输出日历并显示今天是哪一天 1.命令“date”,显示系统的当前日期和时间; 2.命令“date 040100002016”,屏幕显示新修改的系统时间;  #不太明白 3. 转载于:https://www.cnblogs.com/Formulate0303/p/11142997.html...

  • 题目1 --日期 借助随机数,创建一个从1995.1.1 00:00:00 到 1995.12.31 23:59:59 之间的随机日期 package date;import java.util.Date;public class TestDate {public static void main(String[] args) {lo...

  • 一、Date类型的初始化 1、 Date(int year, int month, int date); 直接写入年份是得不到正确的结果的。 因为java中Date是从1900年开始算的,所以前面的第一个参数只要填入从1900年后过了多少年就是你想要得到的年份。 月需要减1,日可以直接插入。 这种方法用的比较少,常用的是第二种方法。...

  • 一.获取当前时间 1.1创建一个日期对象 NSDate *date = [NSDate date];NSLog(@"%@",date);   输出结果: 2016-07-01 17:31:02.410 OCString[907:402963] 2016-07-01 09:31:02 +0000 //因为时...

  • 菜鸟一枚,正在学习C++ Gui Qt4,整理很零碎,欢迎批评指正   1.窗口标题: QWidget *window = new QWidget; window->setWindowTitle("Enter Your Age"); **************************************** 关于标题...

  • 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 总体思路是: 比较两个链表头节点,较小的插入新链表指针之后,同时较小链表指针向后移动一位 实现如下: ListNode* mergeTwo...

  • 1.直接调用微软socket对象处理 static void Main(string[] args){try{IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });//在3721端口新建一个TcpListener对象TcpListener listener = new...

  •   现在很多地方都会用到zookeeper, 用到它的地方就是为了实现分布式。用到的场景就是服务注册,比如一个集群服务器,需要知道哪些服务器在线,哪些服务器不在线。   ZK有一个功能,就是创建临时节点,当机器启动应用的时候就会连接到一个ZK节点,然后创建一个临时节点,那么通过获取监听该路径,并且获取该路径下的节点数量就知道有哪些服务...

  • 前台到后台java时data日期类型的转化 在实体类中用@DataTimeFormat,这样设置即使传过来是空的字符串也是可以转的,要和前面传过来的格式一致,如 @XmlElement(name="BeginDate") @DateTimeFormat(pattern="yyyy-MM-dd") private Date begin...