To learn the Date object dates, first to clarify some knowledge.
How many days are there in one months? How many days are there in a year? What is common year and leap year? What is the difference between common year and leap years? When does the datum point (starting point) of time start? The format of the new Date () initialization?
OK, with these questions, we step into the world of date objects gradually.
One, common year and leap years
The Gregorian year is whole or thousands (that is, at least 2 0 at the end of the year), divided by the year by 400, if divisible, is leap years, not divisible is common year,
Other years of the Gregorian calendar, divided by the year by 4, if divisible, is a leap year, not divisible is common year,
As in 1900, 1900÷400=4 ... 300 is common year, common year February is 28 days.
2000, 2000÷400=5 is leap year, leap year February is 29 days.
2001, 2001÷4=500 ... 1 is common year.
2004, 2004÷4=501 is leap year
Leap year February only 29 days common year February only 28 days
The above paragraph indicates that each year's February is not 28 days is 29 days, and the other months, 1,3,5,7,8,10,12 a total of 7 months are 31 days, 4,6,9,11 total 4 months are 30 days. Therefore, a year is not 365 days, is 366 days. A leap year occurs every four years.
Therefore, leap year is 31*7+29+30*4=366 days, common year is 31*7+28+30*4=365 days.
Two, the beginning of the time
Why does the time begin to count from January 1, 1970? Read the first two articles to find out. First and second, after reading, we can find that the earliest Unix operating system takes into account the age of the computer generation and the time of the application to combine the January 1, 1970 as the time of the Unix era (start time), As for the phenomenon of time regression, I believe that with the 64 of the operating system is gradually resolved, because 64-bit operating system can be represented to 292,277,026,596 December 4 15:30 08 seconds, believe that our generation of n generations, even if the day of the destruction of the earth will not worry enough, Because this time is hundreds of years later. So the rule has been settled. We start with January 1, 1970 as the starting point.
Careful may think, when and how many seconds? Don't worry, we're printing a little bit.
It can be seen from the above that the returned result is January 1, 1970, starting from 8 o'clock. Is that really the case? Actually, seconds and minutes are 0 dot 0 0 seconds ,0 point is the time epoch.
Print out the time is 8 points instead of 0 points, because of the system time and local time problem, in fact, the system time is still 0 points, but my computer time zone is set to the East 8 zone, so the result is 8 points.
Therefore, through the above analysis and exploration, we can draw the following conclusions:
The beginning of the time is January 1, 1970 0:0 0 seconds, but in China due to the computer time zone settings, it will show the printing result is 8 points.
Three: Date initialization format
Date parameter initialization of the new date ()
1) New Date ("Month dd,yyyy Hh:mm:ss") 2) New Date ("Month dd,yyyy"); 3) New Date (YYYY,MTH,DD,HH,MM,SS); 4) New Date (YYYY,MTH,DD); 5) New Date (MS); //note the last form, which represents the number of milliseconds between the time required to create and the GMT time of January 1, 1970
Let's experiment:
1) New Date (' 2014/5/1 00:00:00 ');
2) New Date (' 2014-5-1 00:00:00 ');
3) New Date (1325295549549); Pass the number of milliseconds directly to the new Date (MS)
4) New Date (2014,4,1,00,00,00); If you do not quote in the month minus 1, because the month is starting from 0
5) New Date (' 2014,5,1 ');
6) New Date ("May 1,2014 00:00:00");
7) New Date ("may 1,2014");
There are several ways to return the correct set time. But we have to pay attention to the fourth way of writing, the fourth type, without quotation marks, the second argument refers to the first month, for example, you here 4 is the 4th month, in fact, it is May, because the month is starting from 0. When quoted, it is the equivalent of formatting the time format.
adding quotation marks automatically calls the Date.parse () method, which converts the string to time. If you do not quote, it is directly entered the month and day, and because the index is starting from 0 reasons, naturally will be more than one months of the number of days.
Therefore, when you use the fourth method, remember that the month starts from 0.
var today = new Date (); Get the time now
In this way, every time we refresh the page we get the current moment, that is, the change. If we want it to automatically display the current month and year day, in seconds, we need to write a JS function.
<Bodyonload= "Showtime ()" ><Scriptlanguage= "JavaScript"> functionShowtime () {varToday=NewDate (); varh=today.gethours (); varm=today.getminutes (); vars=today.getseconds (); M=Checktime (m); S=Checktime (s); document.getElementById ('text'). InnerHTML=h+":"+m+":"+s; SetTimeout ('Showtime ()', 2xx); This time as small as possible, because for the sake of precision}functionChecktime (i) {if(i<Ten) {i="0" +i}returni; }</Script><DivID= "text"></Div></Body>
Four: Date initialization format
Is there a need for me to join a mysterious society from May 1, 2014 until now? Ask me how many years and how many months are there in this club?
When we put forward such a requirement, we have to analyze the logic of solving this requirement first. OK, we know the number of milliseconds between the two date segments is definitely accurate. Then you can always accurately calculate how many days, seconds, minutes, etc. And then go up and beg? How many days into the year and month, we know 1 years there may be 365 days or 366 days, the corresponding month has 28|29|30|31 days, do not think it is very complicated to consider the situation. This is certainly not a good idea to deal with. At this time, we need to change a way of thinking. Equivalent processing method.
How is it equivalent? Because we can clearly know the number of milliseconds between two time periods, we know that the base time is calculated starting from 0 o'clock on January 1, 1970. So we can pass the difference in milliseconds to the new date () as its argument, and then find the corresponding time date, which corresponds to subtracting the base time, because the base time is the smallest. So there is no negative value, then a high-level loan of 1.
var today = new Date (), var oldtime = new Date ("2014/5/1 00:00:00");
After getting both of these, we also need to consider the point is getTime ()
var cha = today-oldtime; This is equivalent to Today.gettime ()-Oldtime.gettime (), which implicitly converts the
GetTime () is the number of milliseconds that gets dateobject the specified date and time from Midnight January 1, 1970 (GMT time). What is midnight, that is 12 o'clock in the night, here is from the January 1 night 12 o'clock, that is, January 2 0 o'clock start, so we calculate the difference, and finally excluding January 1 this day, that is the case. Ha ha.
Consider one more question, since our timings are all starting from 0 o'clock, which is in line with our logic, but when we output new Date (0) on the computer; It was found 08:00:00, this is because we are in the East 8 District, the default starting 8 o'clock. So when we calculate the millisecond difference, we have to say that 8*3600*1000 this millisecond is the equivalent of our real time.
var DTIME = 8*3600*1000;
Therefore, after clearing this logic we will be very understanding of the container.
The difference between the two time periods is
var cha = today-oldtime-dtime;
The difference is passed into the new Date ().
var t = new Date (CHA);
And then find the corresponding month and day, time and seconds.
end!!
About JS Date Object two or three things