First, Date Object
The source code that appears below can be codepen online view .
1) Timestamp millisecond calculation
The Date object is based on the number of milliseconds from "1970-01-01 08:00:00" to the specified date, not "00:00:00".
The day is made up of 86,400,000 milliseconds.
var begin = new Date (1970,0,1,0,0,0);//-28800000begin = new Date (1970,0,1,8,0,0);//0
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201701/211606-20170120130645734-1747795077. PNG "width=" "style=" border:0px;margin-top:0px; "/>
You can see the number of milliseconds relative to 8 points by printing the result from the code above.
The timestamp in PHP is seconds, so when interacting with PHP, divide by 1000 and convert to seconds.
2) constructor function
The constructor of date can pass multiple combinations of parameters:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201701/211606-20170120175836625-984370775. PNG "width=" 680 "style=" border:0px;margin-top:0px; "/>
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
var date1 = new Date ("January 20,2017 11:11:05"), var date2 = new Date ("January 20,2017"), var date3 = new Date ("2017/01/20 11:11:05 "), var date4 = new Date (" 2017/01/20 "), var date5 = new Date (2017,0,20,11,11,5); Month from 0~11var date6 = new Date (2017,0,20), var date7 = new Date (1484881865000);
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201701/211606-20170120180319406-1979537667. PNG "width=" 370 "style=" border:0px;margin-top:0px; "/>
Second, Date object method
Here are just a few more ways to refer to theJavaScript Date object
Here is the picture, do not select Copy, want to copy on the address above!
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201701/211606-20170120131248171-252309836. PNG "width=" 713 "style=" border:0px;margin-top:0px; "/>650" this.width=650; "src=" http://images2015.cnblogs.com/ Blog/211606/201701/211606-20170120131330875-125872015.png "width=" 713 "style=" border:0px;margin-top:0px; "/>
The month range is "0-11", not "1-12".
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
var date = new Date (); Gets a Time object var year = Date.getfullyear (); Get the full year (4-bit, 1970) var month = Date.getmonth (); Get the month (0-11, 0 for January, use when remember to add 1) var day = Date.getdate (); Get Day (1-31) var hour = date.gethours (); Gets the number of hours (0-23) var minute = Date.getminutes (); Gets the number of minutes (0-59) var second = Date.getseconds (); Gets the number of seconds (0-59) var week = Date.getday (); Get a day of the week (0-6) where Sunday is 0 weeks six is 6
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201702/211606-20170206183626651-1751213122. PNG "style=" border:0px;margin-top:10px; "/>
Third, date conversion time stamp
1. Human preferred format is "2017-01-20 11:11:05:123";
2. The preferred format for browsers is "2017/01/20 11:11:05:123".
The first format is converted to timestamps, there will be compatibility issues, as shown, more compatibility issues can refer to "JavaScript Date parsing behavior"
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201701/211606-20170120114926437-1711997925. PNG "width=" style= "border:0px;margin-top:0px", "/>650" this.width=650; "src=" http://images2015.cnblogs.com/ Blog/211606/201701/211606-20170120114952015-1652797067.png "width=" "style=" border:0px;margin-top:0px;/>
You can take two steps, the first step is to replace the format with the browser like, the second step is to use the method to get.
1) Format replacement
The following substitution uses a simple regular, and more regular substitutions can be referred to the " fly-up regular expression "
var datestr = ' 2017-01-20 11:11:05:123 ';d ate = new Date (DATESTR); Pass in a time format, if not incoming is to get the time now, so do iOS do Date.parse will return null//another way date = new Date (Datestr.replace (/-/g, '/'));
2) Get time stamp
There are 4 kinds of acquisition methods, implicit type conversion,getTime,valueOf and date.parse.
The first 3 are accurate to milliseconds , and the 4th is only accurate to seconds .
time1 = +date;//implicit type conversion time2 = Date.gettime (); time3 = Date.valueof ();//As with getTime functionality, this method is typically called inside JavaScript, rather than explicitly in code. Time4 = Date.parse (Date);//only accurate to seconds
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201701/211606-20170120135552625-237431539. PNG "width=" style= "border:0px;margin-top:0px";/>
Where implicit conversions are equivalent to the following code:
function () {return number (new Date);}
The above code is converted using the Number object.
There are more methods or objects, such as Boolean, String, number, parsefloat, parseint, ToInt32, and so on, you can refer to theJavascript type-conversion
"+" plus this symbol, you can convert different data types to different values , specific behavior can refer to the "unary + operator"
650) this.width=650; "Src=" Http://images2015.cnblogs.com/blog/211606/201701/211606-20170120140302828-1722485581.png " Style= "border:0px;margin-top:10px;"/>650) this.width=650; src= http://images2015.cnblogs.com/blog/211606/ 201701/211606-20170120140309906-475918139.png "style=" border:0px;margin-top:10px; "/>650) this.width=650;" Src= "Http://images2015.cnblogs.com/blog/211606/201701/211606-20170120140316718-1156589349.png" style= "border:0px; margin-top:10px; "/>
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201701/211606-20170120140613078-1720292057. PNG "style=" border:0px;margin-top:10px; "/>650" this.width=650; "src=" http://images2015.cnblogs.com/blog/211606 /201701/211606-20170120140619250-1385699888.png "style=" border:0px;margin-top:10px; "/>650) this.width=650;" Src= "Http://images2015.cnblogs.com/blog/211606/201701/211606-20170120140404031-328507780.png" style= "border:0px ; margin-top:10px; "/>
Iv. Common Extensions
1) Calculate the number of days apart
Subtract two dates, divided by the conversion of various time units.
Math.Abs (start-end)/60/60/1000/24;
2) The first day of a month
Set the month day, the day is set to 1, and a Date object is eventually generated.
New Date (Date.getfullyear (), Date.getmonth (), 1);
3) The last day of a month
The same is three parameters, but the last day is set to 0, and the month is set to the next one months.
New Date (Date.getfullyear (), Date.getmonth () +1, 0)
4) The first day of the quarter of a month
The month of the date is divided by 3, and then by the symbol "~ ~" gets the integer portion (0, 1, 2, or 3), multiplied by 3 to get to the first month of the quarter.
~ ~ function is to convert a number to a 32-bit signed integer rounding decimal not rounding new Date (Date.getfullyear (), ~ ~ (Date.getmonth ()/3), 1);
5) The last day of the quarter of a month
The calculation is the same as above, the different month is added "3".
New Date (Date.getfullyear (), ~ ~ (Date.getmonth ()/3) + 3, 0)
6) Judging whether it is a leap year
Month set March, the day is set to 0, you can get to the last day of February.
New Date (Date.getfullyear (), 2, 0). getDate () = = 29
7) Number of days in a month
Set the next month for this date, and the number of days is 0.
New Date (Date.getfullyear (), Date.getmonth () +1, 0). GetDate ()
There are often formatting that you can use, such as date.format (' yyyy mm DD day '), which returns "January 20, 2017".
There are many ways to implement the code on the Internet, which is not introduced here.
Riyuerusuo, topsy-JavaScript date