var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
//获得 yyyy-mm-dd HH-mm-ss模式
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
获取前一天 后一天的例子
//调用 getDay(-1,'day','2014-10-12') 前一天; getDay(+1,'day','2014-10-12') 后一天;
// 计算时间
function getDay(n, flag, str) {
var day = strToDate(str);
var curentMonth = day.getMonth() + 1;
var strYear = day.getFullYear();
// 今天几号
var strDate = '';
var strMonth = '';
// 返回结果时间
var strday = '';
if (flag == 'day') {
strDate = day.getDate() + n;
// 跳到下个月
if (strDate > 31) {
strDate = 1;
strMonth = curentMonth + 1;
} // 跳到上个月
else if (strDate < 1) {
strMonth = curentMonth - 1;
if (strMonth == 4 || strMonth == 6 || strMonth == 9
|| strMonth == 11) {
strDate = 30;
} else if (strMonth == 2) {
if ((strYear % 4 == 0 && strYear % 100 != 0)
|| (strYear % 100 == 0 && strYear % 400 == 0)) {
strDate = 29;
} else {
strDate = 28;
}
} else {
strDate = 31;
}
} else {
strMonth = curentMonth;
}
// 跳到下一年
if (strMonth == 13) {
strYear += 1;
strMonth = 1;
}
// 跳到上一年
if (strMonth == 0) {
strYear -= 1;
strMonth = 12;
strDate = 31;
}
// 处理类型2012-01-01格式
strMonth = strMonth < 10 ? "0" + strMonth : strMonth;
strDate = strDate < 10 ? "0" + strDate : strDate;
// 拼接日期字符串
strday = strYear + "-" + strMonth + "-" + strDate;
}
// 计算下个月上个月
if (flag == 'month') {
strMonth = curentMonth + n;
// 处理类型2012-01-01格式
strMonth = strMonth < 10 ? "0" + strMonth : strMonth;
strday = strYear + "-" + strMonth;
}
return strday;
}
// 日期字符串转date
function strToDate(str) {
var regEx = new RegExp("\\-", "gi");
// dependedVal必须是 2014/12/12的格式
dependedVal = str.replace(regEx, "/");
var milliseconds = Date.parse(dependedVal);
var dependedDate = new Date();
dependedDate.setTime(milliseconds);
return dependedDate;
}
获取上一周 下一周的方法
//获取日期差
function getDate(date, dayspan)
{
var time = date.valueOf();
time += (dayspan * 24 * 60 * 60 * 1000);
return new Date(time);
}
// w为0为当前周,1为下周,-1为上周
function getWeek(w) {
var date = new Date();
var x = date.getDay();
var msg = "";
var weekFirst = getDate(date, w * 7 - x+1);
var weekLast = getDate(date, 6 - x + w * 7+1);
var firstMonth = eval(weekFirst.getMonth() + 1)>=10?eval(weekFirst.getMonth() + 1):"0"+eval(weekFirst.getMonth() + 1);
var lastMonth = eval(weekLast.getMonth() + 1)>=10?eval(weekLast.getMonth() + 1):"0"+eval(weekLast.getMonth() + 1);
//一周的开始时间
msg = weekFirst.getFullYear() + "-" + firstMonth
+ "-" + (weekFirst.getDate()>=10?weekFirst.getDate():"0"+weekFirst.getDate());
//一周的结束时间
msg += "=" + weekLast.getFullYear() + "-"
+ lastMonth + "-" + (weekLast.getDate()>=10?weekLast.getDate():"0"+weekLast.getDate());
return msg;
}
获得某年的某月总共有多少天
function getLastDay(year, month) {
var new_year = year;
var new_month = parseInt(month, 10);
var new_date = new Date(new_year, new_month, 1);
return (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate();
}