Date objects can store any date and can be accurate to milliseconds (1/1000 seconds).
Define a Time object:
Note : Use the first letter of the keyword New,date () to capitalize.
Makes udate a Date object and has an initial value: The current time (current computer system time).
If you want to customize the initial values, you can use the following methods:
var d = new Date (1); October 1, 2012 var d = new Date (' OCT 1, 2012 '); October 1, 2012
We'd better use the "method" described below to strictly define the time.
access method Syntax:"< date objects >.< methods >"
Common methods for processing time and dates in a Date object:
Return/Set Year method
get/setFullYear()
Returns/sets the year, expressed as a four-digit number.
var mydate=new Date ();//Current Time March 6, 2014 document.write (mydate+ "<br>");//output Current time document.write ( Mydate.getfullyear () + "<br>");//output Current year mydate.setfullyear (81); Set the year document.write (mydate+ "<br>"); The output year is set to 0081 years.
Note: different browsers, Mydate.setfullyear (81) have different results, and the year is set to 0081 or 812 cases.
Results:
Thu 10:57:47 gmt+08002014thu Mar 0081 10:57:47 gmt+0800
Attention:
1. The result format is: week, month, day, year, time, minute, second, time zone. (Firefox browser)
2. Different browser, time format has difference.
Return Week method
GetDay () returns a number of 0-6 in the week, 0 for Sunday. If you want to return the corresponding "week", complete with the array, the code is as follows:
<script type= "Text/javascript" > var mydate=new date ();//define Date object var weekday=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];//defines the array object, assigns Var mynum=mydate.getday () to each array item, //The return value is stored in the variable mynum document.write ( Mydate.getday ());//output GetDay () Gets the value document.write ("Today is:" + weekday[mynum]);//Output Week </script>
Note: The above code was run on March 7, 2014, Friday.
Results:
5
Today is: Friday
return/Set Time method
get/settime () Returns/sets the time, in milliseconds, to calculate the number of milliseconds from January 1, 1970 0 o'clock to the date the Date object refers to.
If the current date object's time is postponed by 1 hours, the code is as follows:
<script type= "Text/javascript" > var mydate=new Date (); document.write ("Current Time:" +mydate+ "<br>"); Mydate.settime (Mydate.gettime () + * + *); document.write ("Delay One hour:" + mydate);</script>
Results:
Current time: Thu Mar 6 11:46:27 utc+0800 2014
One hour delay: Thu Mar 6 12:46:27 utc+0800 2014
Note: 1. An hour 60 minutes, a minute 60 seconds, a second 1000 milliseconds
2. The time delay is 1 hours, that is: "X.settime (X.gettime () + 60 * 60 * 1000);"
Javascript--date Date Object