0
var myDate = new Date(); console.log(myDate.getYear()); //获取当前年份(2位)(从1900开始) console.log(myDate.getFullYear()); //获取完整的年份(4位,1970-????) console.log(myDate.getMonth()); //获取当前月份(0-11,0代表1月) console.log(myDate.getDate()); //获取当前日(1-31) console.log(myDate.getDay()); //获取当前星期X(0-6,0代表星期天) console.log(myDate.getTime()); //获取当前时间(从1970.1.1开始的毫秒数) console.log(myDate.getHours()); //获取当前小时数(0-23) console.log(myDate.getMinutes()); //获取当前分钟数(0-59) console.log(myDate.getSeconds()); //获取当前秒数(0-59) console.log(myDate.getMilliseconds()); //获取当前毫秒数(0-999) console.log(myDate.toLocaleDateString()); //获取当前日期(2017年1月20日) console.log(myDate.toLocaleTimeString()); //获取当前时间(上午10:15:34) console.log(myDate.toLocaleString()); //获取日期与时间(2017年1月20日 上午10:15:14) //
1
console.log(Date()); //Fri Jan 20 2017 09:38:48 GMT+0800 (中国标准时间)
2
function getLocalTime(nS) {
return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/,' ');
}
alert(getLocalTime((new Date()).getTime()));
//2010年12月23日 上午10:533
function getLocalTime(nS) {
return new Date(parseInt(nS)).toLocaleString().substr(0,17)
}
alert(getLocalTime((new Date()).getTime()));
//Fri Jan 20 2017 04
function getLocalTime(nS) {
return new Date(parseInt(nS)).toLocaleString()
.replace(/年|月/g, "-")
.replace(/日/g, " ")
.replace(/上午/g, "")
.replace(/下午/g, "");
}
var timestamp = Date.parse(new Date()); //获取了当前 秒时间戳(是把毫秒改成000显示)
var timestamp = (new Date()).getTime(); //获取了当前毫秒的时间戳
var timestamp = (new Date()).valueOf(); //获取了当前毫秒的时间戳
alert(getLocalTime(timestamp));
//2017-1-20 9:51:285
function formatDate(now) {
var year=now.getYear()+1900;
var month=now.getMonth()+1;
var date=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}
alert(formatDate(new Date()));
//2017-1-20 9:58:496
//判断闰年
Date.prototype.isLeapYear = function(){
return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
}
console.log(Date.prototype.isLeapYear(Date())); //true/false7
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
Date.prototype.Format = function (fmt) {
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;
}
console.log(new Date().Format("yyyy-MM-dd")); //2017-01-20
console.log(new Date().Format("yyyy-MM-dd hh:mm:ss 第qq季")); //2017-01-20 10:26:54 第01季参考:
http://blog.csdn.net/adsdassadfasdfasdf/article/details/6047250
http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html