1.获取当月的天数
var date = new Date(dt);
new Date(date.getFullYear(), (date.getMonth()+1),0).getDate()
2.获取某一天是星期几
var weekday=new Array(7); weekday[0]="星期日" ; weekday[1]="星期一"; weekday[2]="星期二"; weekday[3]="星期三"; weekday[4]="星期四"; weekday[5]="星期五"; weekday[6]="星期六"; alert("本月第一天是 " + weekday[date.getDay()]);
3.计算时间差
s1 = new Date(s1); s2 = new Date(s2); var days = s2.getTime() - s1.getTime(); var time = parseInt(days / (1000 * 60 * 60 * 24));//计算时间差
4 获取当前 的时间 格式“yyyy-MM-dd HH:MM:SS”
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var seconds=date.getSeconds();
var minutes=date.getMinutes();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + minutes
+ seperator2 + seconds;
return currentdate;
}
5.alert(new Date().toLocaleDateString().replace(///g,"-")) 获取当前时间 2016-1-12
6.2016-01-01 12:59:59:000
function getresidentTime() { var chat_date = new Date(); var chat_seperator1 = "-"; var chat_seperator2 = ":"; var chat_month = chat_date.getMonth() + 1;// 月 var chat_strDate = chat_date.getDate();// 日 var chat_hours=chat_date.getHours();// 时 var chat_minutes=chat_date.getMinutes();// 分getMinutes() var chat_seconds=chat_date.getSeconds();// 秒getSeconds() var chat_milliseconds=chat_date.getMilliseconds();//毫秒 if (chat_month >= 1 && chat_month <= 9) { chat_month = "0" + chat_month; } if (chat_strDate >= 0 && chat_strDate <= 9) { chat_strDate = "0" + chat_strDate; } if (chat_hours >= 0 && chat_hours <= 9) { chat_hours = "0" + chat_hours; } if (chat_minutes >= 0 && chat_minutes <= 9) { chat_minutes = "0" + chat_minutes; } if (chat_seconds >= 0 && chat_seconds <= 9) { chat_seconds = "0" + chat_seconds; } if (chat_milliseconds >= 0 && chat_milliseconds <= 9) { chat_milliseconds = "00" + chat_milliseconds; }else if(chat_milliseconds >9 && chat_milliseconds <= 99){ chat_milliseconds = "0" + chat_milliseconds; } var chat_currentdate = chat_date.getFullYear() + chat_seperator1 + chat_month + chat_seperator1 + chat_strDate+ chat_seperator1+ chat_hours+ chat_seperator1+ chat_minutes+chat_seperator1+ chat_seconds+chat_seperator1+ chat_milliseconds return chat_currentdate; }
获取前几天的时间
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = dd.getMonth() + 1; //获取当前月份的日期
var d = dd.getDate();
return y + "-" + m + "-" + d;
}
GetDateStr(-1);
8 判断时间格式
var aa = new Date(time),
chat_strDate = aa.getDate().toString().length == 2 ? aa.getDate() : ("0" + aa.getDate());
chat_month = (aa.getMonth() + 1).toString().length == 2 ? (aa.getMonth() + 1) : ("0" + (aa.getMonth() + 1));
if(time == undefined || time == "" || isNaN(aa.getDate()) || isNaN(aa.getMonth())) {
return "no";
} else {
return aa.getFullYear() + "-" + chat_month + "-" + chat_strDate;
}
9.获取时间差 天
function publicGetDateDiff(startDate, endDate) {
var startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime();
var endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime();
var dates = Math.abs((startTime - endTime)) / (1000 * 60 * 60 * 24);
return dates;
}
10 获取当前时间的十分
// 获取后 时 分
function getNowFormatDatehs(minutes) {//当前时间的后几分钟
minutes = parseInt(minutes);
var interTimes = minutes * 60 * 1000;
interTimes = parseInt(interTimes);
var chat_date= new Date(Date.parse(getNowFormatDateM()) + interTimes);//getNowFormatDateM() 获取时分秒
var chat_hours=chat_date.getHours();// 时
var chat_minutes=chat_date.getMinutes();// 分getMinutes()
if (chat_hours >= 0 && chat_hours <= 9) {
chat_hours = "0" + chat_hours;
}
if (chat_minutes >= 0 && chat_minutes <= 9) {
chat_minutes = "0" + chat_minutes;
}
return chat_hours+":"+chat_minutes;
}