JS字符串转时间格式:
//字符串转日期格式,strDate要转为日期格式的字符串 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/, function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')'); return date; }
JS计算两个日期之间的天数:
//获取两个日期的天数差 function DayDiff(beginDate, endDate) { var dtBegin = getDate(beginDate); var dtEnd = getDate(endDate); var dayDiff = 0; dayDiff = parseInt(Math.abs(dtEnd - dtBegin) / 1000 / 60 / 60 / 24); return dayDiff; }
JS计算两个时间的月份差:
//获取两个日期的月份差 function MonthDiff(beginDate, endDate) { var dtBegin = getDate(beginDate); var dtEnd = getDate(endDate); var y = dtEnd.getYear() - dtBegin.getYear(); var m = dtEnd.getMonth() - dtBegin.getMonth(); var monthDiff = 0; if (y > 0) { monthDiff = y * 12 + m; } else { monthDiff = m; } return monthDiff; }