最简单的方法使用DateDiff(),判断在两个日期之间存在的指定时间间隔的数目,但是Safari不识别
$(function(){ var beginTime="2015-08-20"; var endTime="2015-12-11"; var days=DateDiff(beginTime,endTime) alert(days) })
第二种方法:
var beginTime="2015-08-20"; var endTime="2015-12-11"; var aDate, oDate1, oDate2, days; aDate = beginTime.split("-"); oDate1 = new Date(aDate[0], aDate[1], aDate[2]); aDate = endTime.split("-"); oDate2 = new Date(aDate[0] , aDate[1] , aDate[2]); days = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
获取当前时间
function addZero(obj) { if (obj < 10) return "0" + obj; else return obj } var currentDate = function () { var mydate = new Date(); var curr_year = mydate.getFullYear(); var curr_month = mydate.getMonth() + 1; var curr_day = mydate.getDate(); var curr_H = mydate.getHours(); var curr_M = mydate.getMinutes(); var curr_S = mydate.getSeconds(); var monthDate = curr_year + "-" + addZero(curr_month) + "-" + addZero(curr_day) + " " +addZero(curr_H)+":"+addZero(curr_M)+":"+addZero(curr_S); return monthDate; } $("#dataTime").val(currentDate());