自己一些总结
日期转换
日期转字符串
1 2 3 4 5 6 7 8 9
| function convertDate2String(datetime){ var year = datetime.getFullYear(); var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1; var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate(); var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours(); var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes(); var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds(); return year + "-" + month + "-" + date+" "+hour+":"+minute; }
|
字符串转日期
1 2
| var oldEndTimeStr = '2017-06-03 11:50'; var oldEndTime = new Date(Date.parse(oldEndTimeStr.replace(/-/g, "/")));
|
日期增减
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| //根据日期字符串计算月份差 (日期格式为"yyyy-MM-dd",根据自己需要改格式和方法) function getMonthNumber(date1,date2){ date1 = date1.split("-"); date2 = date2.split("-"); var year1 = date1[0]; var year2 = date2[0]; var month1 = date1[1]; var month2 = date2[1]; var day1 = date1[2]; var day2 = date2[2]; var len=(year2-year1)*12+(month2-month1); if (day1 >= day2) { len = len - 1; } return len; }
|