创建 var d=new Date(); 要注意的是在JavaScript中月份的值是从0到11(0表示1月)。 设置日期和时间值 设置日期和时间值有两种方法: 1、只声明距离1970年1月1日凌晨12点的毫秒数 a、直接用距离1970年1月1日凌晨12点的毫秒数 var d=new Date(0); b、parse方法: parse方法接受字符串为参数,把该字符串转换成日期值,返回的是毫秒数。 例如为2012年2月27日创建Date对象: var d=new Date(Date.parse("Feb 27,2012")); 如果传给parse方法的字符串不能转换成日期,该函数返回NaN c、UTC方法: UTC方法也返回日期的毫秒表示,但参数为年、月、日、时、分、秒、毫秒,年、月为必选,其他为可选。 例如为2012年2月27日创建Date对象: var d=new Date(Date.UTC(2012,1,27)); 2、直接声明UTC方法接受的参数 var d=new Date(2012,1,27); 参数规则跟UTC方法相同。 Date类方法 Date类方法如下(来自:http://www.jb51.net/w3school/js/jsref_obj_date.htm):
方法
描述
FF
IE
Date()
返回当日的日期和时间。
1
3
getDate()
从 Date 对象返回一个月中的某一天 (1 ~ 31)。
1
3
getDay()
从 Date 对象返回一周中的某一天 (0 ~ 6)。
1
3
getMonth()
从 Date 对象返回月份 (0 ~ 11)。
1
3
getFullYear()
从 Date 对象以四位数字返回年份。
1
4
getYear()
请使用 getFullYear() 方法代替。
1
3
getHours()
返回 Date 对象的小时 (0 ~ 23)。
1
3
getMinutes()
返回 Date 对象的分钟 (0 ~ 59)。
1
3
getSeconds()
返回 Date 对象的秒数 (0 ~ 59)。
1
3
getMilliseconds()
返回 Date 对象的毫秒(0 ~ 999)。
1
4
getTime()
返回 1970 年 1 月 1 日至今的毫秒数。
1
3
getTimezoneOffset()
返回本地时间与格林威治标准时间 (GMT) 的分钟差。
1
3
getUTCDate()
根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
1
4
getUTCDay()
根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
1
4
getUTCMonth()
根据世界时从 Date 对象返回月份 (0 ~ 11)。
1
4
getUTCFullYear()
根据世界时从 Date 对象返回四位数的年份。
1
4
getUTCHours()
根据世界时返回 Date 对象的小时 (0 ~ 23)。
1
4
getUTCMinutes()
根据世界时返回 Date 对象的分钟 (0 ~ 59)。
1
4
getUTCSeconds()
根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
1
4
getUTCMilliseconds()
根据世界时返回 Date 对象的毫秒(0 ~ 999)。
1
4
parse()
返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
1
3
setDate()
设置 Date 对象中月的某一天 (1 ~ 31)。
1
3
setMonth()
设置 Date 对象中月份 (0 ~ 11)。
1
3
setFullYear()
设置 Date 对象中的年份(四位数字)。
1
4
setYear()
请使用 setFullYear() 方法代替。
1
3
setHours()
设置 Date 对象中的小时 (0 ~ 23)。
1
3
setMinutes()
设置 Date 对象中的分钟 (0 ~ 59)。
1
3
setSeconds()
设置 Date 对象中的秒钟 (0 ~ 59)。
1
3
setMilliseconds()
设置 Date 对象中的毫秒 (0 ~ 999)。
1
4
setTime()
以毫秒设置 Date 对象。
1
3
setUTCDate()
根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
1
4
setUTCMonth()
根据世界时设置 Date 对象中的月份 (0 ~ 11)。
1
4
setUTCFullYear()
根据世界时设置 Date 对象中的年份(四位数字)。
1
4
setUTCHours()
根据世界时设置 Date 对象中的小时 (0 ~ 23)。
1
4
setUTCMinutes()
根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
1
4
setUTCSeconds()
根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
1
4
setUTCMilliseconds()
根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
1
4
toSource()
返回该对象的源代码。
1
-
toString()
把 Date 对象转换为字符串。
1
4
toTimeString()
把 Date 对象的时间部分转换为字符串。
1
4
toDateString()
把 Date 对象的日期部分转换为字符串。
1
4
toGMTString()
请使用 toUTCString() 方法代替。
1
3
toUTCString()
根据世界时,把 Date 对象转换为字符串。
1
4
toLocaleString()
根据本地时间格式,把 Date 对象转换为字符串。
1
3
toLocaleTimeString()
根据本地时间格式,把 Date 对象的时间部分转换为字符串。
1
3
toLocaleDateString()
根据本地时间格式,把 Date 对象的日期部分转换为字符串。
1
3
UTC()
根据世界时返回 1997 年 1 月 1 日 到指定日期的毫秒数。
1
3
valueOf()
返回 Date 对象的原始值。
1
4分享一个日期格式化方法 在这儿分享一个日期格式化方法,使用方法跟C#中DateTime的ToString方法类似: 复制代码 代码如下: Date.prototype.toString=function(format){ var time={}; time.Year=this.getFullYear(); time.TYear=(""+time.Year).substr(2); time.Month=this.getMonth()+1; time.TMonth=time.Month<10?"0"+time.Month:time.Month; time.Day=this.getDate(); time.TDay=time.Day<10?"0"+time.Day:time.Day; time.Hour=this.getHours(); time.THour=time.Hour<10?"0"+time.Hour:time.Hour; time.hour=time.Hour<13?time.Hour:time.Hour-12; time.Thour=time.hour<10?"0"+time.hour:time.hour; time.Minute=this.getMinutes(); time.TMinute=time.Minute<10?"0"+time.Minute:time.Minute; time.Second=this.getSeconds(); time.TSecond=time.Second<10?"0"+time.Second:time.Second; time.Millisecond=this.getMilliseconds(); var oNumber=time.Millisecond/1000; if(format!=undefined && format.replace(/s/g,"").length>0){ format=format .replace(/yyyy/ig,time.Year) .replace(/yyy/ig,time.Year) .replace(/yy/ig,time.TYear) .replace(/y/ig,time.TYear) .replace(/MM/g,time.TMonth) .replace(/M/g,time.Month) .replace(/dd/ig,time.TDay) .replace(/d/ig,time.Day) .replace(/HH/g,time.THour) .replace(/H/g,time.Hour) .replace(/hh/g,time.Thour) .replace(/h/g,time.hour) .replace(/mm/g,time.TMinute) .replace(/m/g,time.Minute) .replace(/ss/ig,time.TSecond) .replace(/s/ig,time.Second) .replace(/fff/ig,time.Millisecond) .replace(/ff/ig,oNumber.toFixed(2)*100) .replace(/f/ig,oNumber.toFixed(1)*10); } else{ format=time.Year+"-"+time.Month+"-"+time.Day+" "+time.Hour+":"+time.Minute+":"+time.Second; } return format; } var d=new Date(); console.log(d.toString()); //2011-12-29 11:29:43 console.log(d.toString("")); //2011-12-29 11:29:43 console.log(d.toString("yyyy-MM-dd")); //2011-12-29 console.log(d.toString("HH:mm:ss")); //11:29:43 console.log(d.toString("yyyy-MM-dd HH:mm:ss")); //2011-12-29 11:29:43 console.log(d.toString("yyyy年MM月dd日 HH:mm:ss")); //2011年12月29日 11:29:43 console.log(d.toString("yyyy-MM-dd HH:mm:ss fff")); //2011-12-29 11:29:43 862
推荐阅读
JavaScript高级程序设计阅读笔记(六) ECMAScript中的运算符(二)
2.9.5、加性运算符 加性运算符(即加号和减号)通常是最简单的运算符,不过在ECMAScript中,每个加性运算符都有大量的特殊行为。 1、加法运算符: 复制代码 代码如下: var iResult=1+2; console.log(iResu>>>详细阅读
本文标题:JavaScript高级程序设计 读书笔记之十 本地对象Date日期
地址:http://www.17bianji.com/kaifa2/JS/23598.html
1/2 1