Date Object Properties
日期对象方法
FF: Firefox, N: Netscape, IE: Internet Explorer
FF:火狐,N:网景,IE
Method 方法 |
Description 描述 |
FF | N | IE |
---|---|---|---|---|
Date() | Returns today's date and time 返回今天的日期和时间 |
1 | 2 | 3 |
getDate() | Returns the day of the month from a Date object (from 1-31) 返回月中的第几天(1到31) |
1 | 2 | 3 |
getDay() | Returns the day of the week from a Date object (from 0-6) 返回一周中的第几天(0到6) |
1 | 2 | 3 |
getMonth() | Returns the month from a Date object (from 0-11) 返回月份数(0到11) |
1 | 2 | 3 |
getFullYear() | Returns the year, as a four-digit number, from a Date object 返回完整的年份数 |
1 | 4 | 4 |
getYear() | Returns the year, as a two-digit or a four-digit number, from a Date object. Use getFullYear() instead !! 返回年份,可以是两位的或是四位的 |
1 | 2 | 3 |
getHours() | Returns the hour of a Date object (from 0-23) 返回日期对象的小时数(0到23) |
1 | 2 | 3 |
getMinutes() | Returns the minutes of a Date object (from 0-59) 返回日期对象的分钟(0到59) |
1 | 2 | 3 |
getSeconds() | Returns the seconds of a Date object (from 0-59) 返回日期对象的秒(0到59) |
1 | 2 | 3 |
getMilliseconds() | Returns the milliseconds of a Date object (from 0-999) 返回毫秒(0到999) |
1 | 4 | 4 |
getTime() | Returns the number of milliseconds since midnight Jan 1, 1970 从1970年1月1号午夜到现在一共花去的毫秒数 |
1 | 2 | 3 |
getTimezoneOffset() | Returns the difference in minutes between local time and Greenwich Mean Time (GMT) 本地时间和GMT相差多少分钟 |
1 | 2 | 3 |
getUTCDate() | Returns the day of the month from a Date object according to universal time (from 1-31) 依据国际时间来得到月中的第几天(1到31) |
1 | 4 | 4 |
getUTCDay() | Returns the day of the week from a Date object according to universal time (from 0-6) 依据国际时间来得到现在是星期几(0到6) |
1 | 4 | 4 |
getUTCMonth() | Returns the month from a Date object according to universal time (from 0-11) 依据国际时间来得到月份(0到11) |
1 | 4 | 4 |
getUTCFullYear() | Returns the four-digit year from a Date object according to universal time 依据国际时间来得到完整的年份 |
1 | 4 | 4 |
getUTCHours() | Returns the hour of a Date object according to universal time (from 0-23) 依据国际时间来得到小时(0-23) |
1 | 4 | 4 |
getUTCMinutes() | Returns the minutes of a Date object according to universal time (from 0-59) 依据国际时间来返回分钟(0到59) |
1 | 4 | 4 |
getUTCSeconds() | Returns the seconds of a Date object according to universal time (from 0-59) 依据国际时间来返回秒(0到59) |
1 | 4 | 4 |
getUTCMilliseconds() | Returns the milliseconds of a Date object according to universal time (from 0-999) 依据国际时间来返回毫秒(0到999) |
1 | 4 | 4 |
parse() | Takes a date string and returns the number of milliseconds since midnight of January 1, 1970 或得并返回自1970年1月1号凌晨到现在一共花掉了多少毫秒 |
1 | 2 | 3 |
setDate() | Sets the day of the month in a Date object (from 1-31) 设置日 |
1 | 2 | 3 |
setMonth() | Sets the month in a Date object (from 0-11) 设置月 |
1 | 2 | 3 |
setFullYear() | Sets the year in a Date object (four digits) 设置年份 |
1 | 4 | 4 |
setYear() | Sets the year in the Date object (two or four digits). Use setFullYear() instead !! 用setFullYear()来取代 |
1 | 2 | 3 |
setHours() | Sets the hour in a Date object (from 0-23) 设置小时 |
1 | 2 | 3 |
setMinutes() | Set the minutes in a Date object (from 0-59) 设置分钟 |
1 | 2 | 3 |
setSeconds() | Sets the seconds in a Date object (from 0-59) 设置秒 |
1 | 2 | 3 |
setMilliseconds() | Sets the milliseconds in a Date object (from 0-999) 设置毫秒 |
1 | 4 | 4 |
setTime() | Calculates a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970 | 1 | 2 | 3 |
setUTCDate() | Sets the day of the month in a Date object according to universal time (from 1-31) 依据国际时间来设置日期 |
1 | 4 | 4 |
setUTCMonth() | Sets the month in a Date object according to universal time (from 0-11) 依据国际时间来设置月 |
1 | 4 | 4 |
setUTCFullYear() | Sets the year in a Date object according to universal time (four digits) 依据国际时间来设置年份 |
1 | 4 | 4 |
setUTCHours() | Sets the hour in a Date object according to universal time (from 0-23) 依据国际时间来设置小时 |
1 | 4 | 4 |
setUTCMinutes() | Set the minutes in a Date object according to universal time (from 0-59) 依据国际时间来设置分钟 |
1 | 4 | 4 |
setUTCSeconds() | Set the seconds in a Date object according to universal time (from 0-59) 依据国际时间来设置秒 |
1 | 4 | 4 |
setUTCMilliseconds() | Sets the milliseconds in a Date object according to universal time (from 0-999) 依据国际时间来设置毫秒 |
1 | 4 | 4 |
toSource() | Represents the source code of an object 显示对象的源代码 |
1 | 4 | - |
toString() | Converts a Date object to a string 将日期对象转换为字符串 |
1 | 2 | 4 |
toGMTString() | Converts a Date object, according to Greenwich time, to a string. Use toUTCString() instead !! 根据格林威治时间将Date[日期]对象转换为一个字符串。可以使用toUTCString()替代这种方法 |
1 | 2 | 3 |
toUTCString() | Converts a Date object, according to universal time, to a string 根据通用时间将一个Date[日期]对象转换为一个字符串 |
1 | 4 | 4 |
toLocaleString() | Converts a Date object, according to local time, to a string 根据本地时间将一个Date[日期]对象转换为一个字符串 |
1 | 2 | 3 |
UTC() | Takes a date and returns the number of milliseconds since midnight of January 1, 1970 according to universal time 根据通用时间将日期计算为从1970年1月1日午夜至今所经过的时间(单位:毫秒) |
1 | 2 | 3 |
valueOf() | Returns the primitive value of a Date object 返回日期对象的原始值 |
1 | 2 | 4 |
Property 属性 |
Description 描述 |
FF | N | IE |
---|---|---|---|---|
constructor | A reference to the function that created the object 所建立对象的函数参考 |
1 | 4 | 4 |
prototype | Allows you to add properties and methods to the object 能够为对象加入的属性和方法 |
1 | 3 | 4 |
1、对类似“2002-03-19 11:33”这样的日期进行加减:
var date = new Date("2002-03-19 11:33");
//1 day=24×60×60 = 86400 seconds
//加一天
var d = date.getTime() + 24*60*60;
var newdate = new Date(d);
2、日期减去天数等于第二个日期
<script language=Javascript>
function cc(dd,dadd)
{
//可以加上错误处理
var a = new Date(dd)
a = a.valueOf()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date(a)
alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
}
cc("12/23/2002",2)
</script>
这里不得不做补充,浪费好多时间得出教训:
Javascript 对时间的代号
0-11数字表示1-12月: var a= new Date(2006,5,6) 结果是2006-6-6
0-6表示星期
1-31表示日期
0-23小时
0-59分钟,秒
3、自己写的一个测试页date.html:js日期加N天
<html> <head> <title>js日期增加N天</title> </head> <script type="text/javascript"> function addDateStr(dateStr,days){ var year = parseInt(dateStr.substring(0,4)); var month = parseInt(dateStr.substring(5,7),10)-1; var day = parseInt(dateStr.substring(8,10),10); alert("year :["+year+"]"+"\nmonth :["+month+"]"+ "\nday :["+day+"]"); var date = new Date(year,month,day); var d = date.getTime() + days*24*60*60*1000; var newDate= new Date(d); year=newDate.getFullYear(); month=newDate.getMonth()+1; if(month<10){ month="0"+month; } day=newDate.getDate(); if(day<10){ day="0"+day; } alert("year :["+year+"]"+"\nmonth :["+month+"]"+ "\nday :["+day+"]"); var newDateStr=year+"-"+month+"-"+day; return newDateStr; } function test(){ alert(addDateStr(document.getElementById("txtTest").value,document.getElementById ("txtDay").value)); } </script> <body> <input type="text" id="txtTest" value="2008-02-28"> <input type="text" id="txtDay" value="1"> <input type="button" id="btnTest" value="test" onclick="test()"> </body> </html>