1. 日期
var oTime = new Date();// 也可以去掉new
document.write(oTime);
输出:
即:
sun(周日)Nov(11月)2015(年)23:28:20(时间) GMT+0800 (中国标准时间)
其中08即为东8区,即北京时间。
var oTime = new Date();// 也可以去掉new
var year = oTime.getYear();// 缩略版的年份,如:115
var fullYear = oTime.getFullYear();// 完整的年份,如:2015
var month = oTime.getMonth()+1;// 月份,0-11对应1-12月
var date = oTime.getDate();// 日,1-31
var day = oTime.getDay();// 星期:0-6对应周日到周六
document.write("年月日:"+fullYear+"-"+month+"-"+date+";周"+day);
输出:年月日:2015-11-15;周0
1) 汇总:
var date = new Date();
date.getDate(); // 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
date.getDay(); // 从 Date 对象返回一周中的某一天 (0 ~ 6)。
date.getMonth(); // 从 Date 对象返回月份 (0 ~ 11)。
date.getFullYear(); // 从 Date 对象以四位数字返回年份。
date.getHours(); // 返回 Date 对象的小时 (0 ~ 23)。
date.getMinutes(); // 返回 Date 对象的分钟 (0 ~ 59)。
date.getSeconds(); // 返回 Date 对象的秒数 (0 ~ 59)。
date.getMilliseconds(); //返回 Date 对象的毫秒(0 ~ 999)。
date.getTime();// 方法返回从1970-01-01开始到现在的毫秒数
2) 格式化输出:
可以像上面一样使用各个函数获取年月日,使用-拼起来,只是这样代码比较多,现使用格式化输出,定义一个插件js。
使用方法如下:
<script type="text/javascript" src="Tmutil.js"></script>
<script type="text/javascript">
var oTime = new Date();
document.write(oTime.format("yyyy-MM-dd hh:mm:ss"));
document.write("<br/>");
document.write(oTime.format("yyyy-MM-dd HH:mm:ss"));
</script>
输出如下:
如:js插件如下:
/**
* 对Date的扩展,将 Date 转化为指定格式的String 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)
* 可以用 1-2 个占位符 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) eg: (new
* Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 (new
* Date()).format("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04 (new
* Date()).format("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04 (new
* Date()).format("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04 (new
* Date()).format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
*/
Date.prototype.format = function(fmt) {
var o = {
"M+": this.getMonth() + 1,
// 月份
"d+": this.getDate(),
// 日
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12,
// 小时
"H+": this.getHours(),
// 小时
"m+": this.getMinutes(),
// 分
"s+": this.getSeconds(),
// 秒
"q+": Math.floor((this.getMonth() + 3) / 3),
// 季度
"S": this.getMilliseconds() // 毫秒
};
var week = {
"0": "/u65e5",
"1": "/u4e00",
"2": "/u4e8c",
"3": "/u4e09",
"4": "/u56db",
"5": "/u4e94",
"6": "/u516d"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f": "/u5468") : "") + week[this.getDay() + ""]);
}
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;
};
3) 构造函数:
1.使用time值,即从1970-01-01 开始后的毫秒数
例:
var oTime1 = new Date(0);
document.write("<br/>"+oTime1);
var oTime2 = new Date(1000*60*60*2+1000);
document.write("<br/>"+oTime2);
输出:
Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间)
Thu Jan 01 1970 10:00:01 GMT+0800 (中国标准时间)
第一个date为基准时间:1970年1月1日,早8点。
第二个date为增加2小时1秒,即早10点零1秒。
2.使用字串
var oTime3 = new Date("2015-11-16 8:20:4");
-这种方式只有火狐才能识别。
var oTime3 = new Date("2015/11/16 8:20:4");
/这种方式为所有浏览器通用
如:
var sData = "2015-11-16 8:20:4";
var oTime4 = new Date(sData);
document.write("<br/><br/>"+oTime4);
输出:
因此需要转换一下:
var sData = "2015-11-16 8:20:4";
sData = sData.replace(/-/g,"/");
/g的意思是全局替换,不加的话只会替换第一个。
或者使用正则也可以。
Date.parse(string)方法并不是返回Date,而是返回整数
Date.parse("Jul 8, 2005");
Date.parse("2005-07-08");
Date.parse("2005/07/08");
toString(); // Tue Sep 11 2012 15:09:01 GMT+0800
toTimeString(); // 15:09:34 GMT+0800
toDateString(); // Tue Sep 11 2012
toGMTString(); // Tue, 11 Sep 2012 07:10:05 GMT
toUTCString(); // Tue, 11 Sep 2012 07:10:17 GMT
toLocaleString(); // 2012年9月11日 15:10:31
toLocaleTimeString(); // 15:10:45
toLocaleDateString(); // 2012年9月11日
Set系列方法和Get系列方法一一对应,如:
date.setDate(24);
date.setUTCDate(24);
date.setTime(time)方法以毫秒数设置Date对象,和new Date(time)的作用一样。