JS时间对象
取当前时间
1
| var nowTime = new Date();
|
取年、月、日
1 2 3
| nowTime.getFullYear(); nowTime.getMonth() + 1; nowTime.getDate();
|
其他方法 get
getTime():返回从 1970 年 1 月 1 日至今的毫秒数。
getDay(): 返回当前是星期几,返回值范围 (0-6)。
getHours(): 返回时,返回值范围 (0-23)。
getMinutes():返回分,返回值范围 (0-59)。
getSeconds():返回秒,返回值范围 (0-59)。
getMilliseconds():返回毫秒,返回值范围 (0-999)。
其他方法 set
setFullYear():设置 Date 对象中的年份(四位数字)。
setMonth():设置 Date 对象中的月份,取值范围 (0 ~ 11)。
setDate():设置 Date 对象中月的某一天,取值范围 (1 ~ 31)。
setHours():设置 Date 对象中的时,取值范围 (0 ~ 23)。
setMinutes():设置 Date 对象中的分,取值范围 (0 ~ 59)。
setSeconds():设置 Date 对象中的秒,取值范围 (0 ~ 59)。
setMilliseconds():设置 Date 对象中的毫秒,取值范围 (0 ~ 999)。
setTime():以毫秒设置 Date 对象。
格式化时间格式常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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; }
|