1.在已知日期上加天数返回新日期
var day1 = 2016/5/10;
var date1 = 5;
date1 = new Date(date1); //Tue May 10 2016 00:00:00 GMT+0800 (中国标准时间)
var millSeconds = Math.abs(date1)+(day1*24*60*60*1000); //1463241600000
var rdate = new Date(millSeconds); //Sun May 15 2016 00:00:00 GMT+0800 (中国标准时间)
var year = rdate.getFullYear();
var month = rdate.getMonth() + 1;
if (month < 10){
month = "0" + month;
}
var date = rdate.getDate();
if (date < 10) {
date = "0" + date;
}
return year+"-"+month+"-"+date;
2.返回两个日期之间的天数
var date2 = Date.parse("2016/5/15");
var date3 = Date.parse("2016/5/10");
return Math.floor((date2 - date3) / (24 * 60 * 60 * 1000));
3、js里的时间格式
Date.parse(dateVal):解析一个包含日期的字符串,并返回该日期与1970年1月1日午夜之间所间隔的毫秒数。
var date_serial = "2016/05-08 12:03:04";
var date = new Date(date_serial); date = Sun May 08 2016 12:03:04 GMT+0800 (中国标准时间);
var s1 = date.toDateString(); s1 = "Sun May 08 2016";
var s2 = date.toGMTString(); s2 = "Sun, 08 May 2016 04:03:04 GMT"
var s3 = date.toLocaleDateString(); s3 = "2016/5/8"
var s4 = date.toLocaleString(); s4 = "2016/5/8 下午12:03:04"
var s5 = date.toLocaleTimeString(); s5 = "2016/5/8 下午12:03:04"
var s6 = date.toTimeString(); s6 = "12:03:04 GMT+0800 (中国标准时间)"
注意:在ie浏览器,不识别“2016-05-08 12:03:04”格式的日期,但识别“2016/05/08 12:03:04”
var day1 = 2016/5/10;
var date1 = 5;
date1 = new Date(date1); //Tue May 10 2016 00:00:00 GMT+0800 (中国标准时间)
var millSeconds = Math.abs(date1)+(day1*24*60*60*1000); //1463241600000
var rdate = new Date(millSeconds); //Sun May 15 2016 00:00:00 GMT+0800 (中国标准时间)
var year = rdate.getFullYear();
var month = rdate.getMonth() + 1;
if (month < 10){
month = "0" + month;
}
var date = rdate.getDate();
if (date < 10) {
date = "0" + date;
}
return year+"-"+month+"-"+date;
2.返回两个日期之间的天数
var date2 = Date.parse("2016/5/15");
var date3 = Date.parse("2016/5/10");
return Math.floor((date2 - date3) / (24 * 60 * 60 * 1000));
3、js里的时间格式
Date.parse(dateVal):解析一个包含日期的字符串,并返回该日期与1970年1月1日午夜之间所间隔的毫秒数。
var date_serial = "2016/05-08 12:03:04";
var date = new Date(date_serial); date = Sun May 08 2016 12:03:04 GMT+0800 (中国标准时间);
var s1 = date.toDateString(); s1 = "Sun May 08 2016";
var s2 = date.toGMTString(); s2 = "Sun, 08 May 2016 04:03:04 GMT"
var s3 = date.toLocaleDateString(); s3 = "2016/5/8"
var s4 = date.toLocaleString(); s4 = "2016/5/8 下午12:03:04"
var s5 = date.toLocaleTimeString(); s5 = "2016/5/8 下午12:03:04"
var s6 = date.toTimeString(); s6 = "12:03:04 GMT+0800 (中国标准时间)"
注意:在ie浏览器,不识别“2016-05-08 12:03:04”格式的日期,但识别“2016/05/08 12:03:04”