JS timestamp conversion date format and date calculation

Source: Internet
Author: User
Tags string format

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
  1. Date () returns the day and time.
  2. getDate () returns the day of the one month (1 ~ 31) from the Date object.
  3. GetDay () returns the day of the week (0 ~ 6) from the Date object.
  4. getMonth () returns the month (0 ~ 11) from the Date object.
  5. getfullyear () returns the year as a four-digit number from a Date object.
  6. getYear () please use the getFullYear () method instead.
  7. getHours () returns the hour (0 ~ 23) of the Date object.
  8. getminutes () returns the minute (0 ~ 59) of the Date object.
  9. getseconds () returns the number of seconds (0 ~ 59) of the Date object.
  10. getmilliseconds () returns the milliseconds (0 ~ 999) of the Date object.
  11. getTime () returns the number of milliseconds since January 1, 1970.
  12. gettimezoneoffset () returns the minute difference between local time and Greenwich Mean Time (GMT).
  13. getutcdate () returns the day of the month (1 ~ 31) from the Date object according to the world.
  14. GetUTCDay () returns the day of the week (0 ~ 6) from the Date object according to the world.
  15. getutcmonth () returns the month (0 ~ 11) from the Date object, depending on the world.
  16. getutcfullyear () returns a four-digit year from a Date object according to the world.
  17. getutchours () returns the hour (0 ~ 23) of the Date object according to the universal.
  18. getutcminutes () returns the minute (0 ~ 59) of the date object according to the universal.
  19. getutcseconds () returns the second (0 ~ 59) of the Date object according to the universal.
  20. getutcmilliseconds () returns the milliseconds (0 ~ 999) of a Date object based on the universal.
  21. Parse () returns the number of milliseconds from midnight January 1, 1970 to the specified date (string).
  22. setDate () sets the day of the month in the Date object (1 ~ 31).
  23. setmonth () sets the month (0 ~ 11) in the Date object.
  24. setfullyear () sets the year (four digits) in the Date object.
  25. setyear () please use the setFullYear () method instead.
  26. sethours () sets the hour (0 ~ 23) in the Date object.
  27. setminutes () sets the minute (0 ~ 59) in the Date object.
  28. setseconds () sets the second (0 ~ 59) in the Date object.
  29. setmilliseconds () sets the milliseconds (0 ~ 999) in the Date object.
  30. settime () sets the Date object in milliseconds.
  31. setUTCDate () sets the day of the month in the Date object according to the world time (1 ~ 31).
  32. setUTCMonth () sets the month (0 ~ 11) in the Date object according to the world time.
  33. setutcfullyear () sets the year (four digits) in the Date object according to the world time.
  34. setutchours () sets the hour (0 ~ 23) in the Date object according to the world time.
  35. setutcminutes () sets the minute (0 ~ 59) in the Date object according to the world time.
  36. setutcseconds () sets the seconds (0 ~ 59) in the Date object according to the world time.
  37. setutcmilliseconds () sets the milliseconds (0 ~ 999) in the Date object according to the world time.
  38. Tosource () returns the source code of the object.
  39. toString () converts the Date object to a string.
  40. totimestring () converts the time portion of a Date object to a string.
  41. todatestring () converts the date part of a Date object to a string.
  42. togmtstring () please use the toutcstring () method instead.
  43. toutcstring () converts the Date object to a string based on the universal.
  44. tolocalestring () converts a Date object to a string based on the local time format.
  45. tolocaletimestring () converts the time portion of a Date object to a string, based on the local time format.
  46. tolocaledatestring () converts the date part of a Date object to a string, based on the local time format.
  47. UTC () returns the number of milliseconds from January 1, 1970 to the specified date, depending on the time.
  48. valueOf () returns the original value of the Date object.

JS timestamp conversion date format and date calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.