One, time stamp conversion date
1 functionformatdate (datetime) {2 //Slice (-2) filters out more than 0 in front of the 10th period when the month day is obtained3 varYear =datetime.getfullyear (),4Month = ("0" + (DateTime.)GetMonth() + 1)). Slice (-2),5Date = ("0" + DateTime.)getDate()). Slice (-2),6Hour = ("0" + DateTime.)getHours()). Slice (-2),7minute = ("0" + DateTime.)getminutes()). Slice (-2),8Second = ("0" + DateTime.)getseconds()). Slice (-2);9 //StitchingTen varresult = year + "-" + month + "-" + Date + "" + Hour + ":" + Minute + ":" +second; One //return A returnresult; - } - the varDate =NewDate (); -Console.log (formatdate(date));//2018-05-26 23:09:26
Ii. calculation of the contract date
Calculates the end date based on the start date and period
1 //Date: Day string yyyy-mm-dd, such as: 2016-02-142 //years: Year, positive integer string3 //returns a date string yyyy-mm-dd, such as: 2016-02-144 functiondateaddyear (date, years) {5 varnow =Newdate (date);6 varIntyear = Now.getFullYear() +parseint (years);7 varIntmonth = Now.GetMonth() + 1;//the normal month,8 varIntday = Now.getDate()-1;//Date-19 if(Intday = = 0) {Tenintmonth--;//one months reduced One if(Intmonth = = 0) { Aintyear--;//0: One year reduction -Intmonth = 12; -Intday = 31; the } - Else if(Intmonth = = 4 | | intmonth = = 6 | | intmonth = = 9 | | intmonth = = 11) { -Intday = 30;//4,6,9,11:30 days - } + Else if(Intmonth = = 2) { -Intday = 28;//2:28/29 + if(intyear% 4 = = 0) { AIntday = 29; at } -}Else { -Intday = 31;//1,3,5,7,8,10,12:31 days - } - } - in varStrmonth = (Intmonth) < 10? "0" +(Intmonth). ToString (): (Intmonth). ToString (); - varStrday = (Intday) < 10? "0" +(Intday). ToString (): (Intday). ToString (); to varStrenddate = intyear + "-" + Strmonth + "-" +Strday; + returnstrenddate; - } the *Console.log (Dateaddyear(' 2018-6-10 ', ' 2 '));//2020-06-09
Third, calculate the date after count days according to the start date
begindate is the start date, string format
Count refers to the number of days, integers
Note: setdate and getdate are used together
Date. setDate (date. getDate () + count);
1 functioncalculatedate (begindate,count) {2 varDate =NewDate (begindate);3 date.setdate (date.getdate () + count);4 varYear =date. getFullYear ();5 varMonth = ("0" + (date.)GetMonth() +1). Slice (-2);6 varDay = ("0" + date.)getDate()). Slice (-2);7 varHours = ("0" + date.)getHours()). Slice (-2);8 varminute = ("0" + date.)getminutes()). Slice (-2);9 varSecond = ("0" + date.)getseconds()). Slice (-2);Ten One varEndDate = year + "-" + month + "-" + Day + "" + Hours + ":" + Minute + ":" +second; A - returnendDate; - } the -Console.log (calculatedate(' 2018-5-26 23:50:32 ', 30));//2018-06-25 23:50:32
Iv. commonly used Date object methods
- Date () returns the day and time.
- getDate () returns the day of the one month (1 ~ 31) from the Date object.
- GetDay () returns the day of the week (0 ~ 6) from the Date object.
- getMonth () returns the month (0 ~ 11) from the Date object.
- getfullyear () returns the year as a four-digit number from a Date object.
- getYear () please use the getFullYear () method instead.
- getHours () returns the hour (0 ~ 23) of the Date object.
- getminutes () returns the minute (0 ~ 59) of the Date object.
- getseconds () returns the number of seconds (0 ~ 59) of the Date object.
- getmilliseconds () returns the milliseconds (0 ~ 999) of the Date object.
- getTime () returns the number of milliseconds since January 1, 1970.
- gettimezoneoffset () returns the minute difference between local time and Greenwich Mean Time (GMT).
- getutcdate () returns the day of the month (1 ~ 31) from the Date object according to the world.
- GetUTCDay () returns the day of the week (0 ~ 6) from the Date object according to the world.
- getutcmonth () returns the month (0 ~ 11) from the Date object, depending on the world.
- getutcfullyear () returns a four-digit year from a Date object according to the world.
- getutchours () returns the hour (0 ~ 23) of the Date object according to the universal.
- getutcminutes () returns the minute (0 ~ 59) of the date object according to the universal.
- getutcseconds () returns the second (0 ~ 59) of the Date object according to the universal.
- getutcmilliseconds () returns the milliseconds (0 ~ 999) of a Date object based on the universal.
- Parse () returns the number of milliseconds from midnight January 1, 1970 to the specified date (string).
- setDate () sets the day of the month in the Date object (1 ~ 31).
- setmonth () sets the month (0 ~ 11) in the Date object.
- setfullyear () sets the year (four digits) in the Date object.
- setyear () please use the setFullYear () method instead.
- sethours () sets the hour (0 ~ 23) in the Date object.
- setminutes () sets the minute (0 ~ 59) in the Date object.
- setseconds () sets the second (0 ~ 59) in the Date object.
- setmilliseconds () sets the milliseconds (0 ~ 999) in the Date object.
- settime () sets the Date object in milliseconds.
- setUTCDate () sets the day of the month in the Date object according to the world time (1 ~ 31).
- setUTCMonth () sets the month (0 ~ 11) in the Date object according to the world time.
- setutcfullyear () sets the year (four digits) in the Date object according to the world time.
- setutchours () sets the hour (0 ~ 23) in the Date object according to the world time.
- setutcminutes () sets the minute (0 ~ 59) in the Date object according to the world time.
- setutcseconds () sets the seconds (0 ~ 59) in the Date object according to the world time.
- setutcmilliseconds () sets the milliseconds (0 ~ 999) in the Date object according to the world time.
- Tosource () returns the source code of the object.
- toString () converts the Date object to a string.
- totimestring () converts the time portion of a Date object to a string.
- todatestring () converts the date part of a Date object to a string.
- togmtstring () please use the toutcstring () method instead.
- toutcstring () converts the Date object to a string based on the universal.
- tolocalestring () converts a Date object to a string based on the local time format.
- tolocaletimestring () converts the time portion of a Date object to a string, based on the local time format.
- tolocaledatestring () converts the date part of a Date object to a string, based on the local time format.
- UTC () returns the number of milliseconds from January 1, 1970 to the specified date, depending on the time.
- valueOf () returns the original value of the Date object.
JS timestamp conversion date format and date calculation