JS date operations

Source: Internet
Author: User
  1. Var now = new Date (); // current Date
  2. Var nowDayOfWeek = now. getDay (); // the day of the week today
  3. Var nowDay = now. getDate (); // current day
  4. Var nowMonth = now. getMonth (); // current month
  5. Var nowYear = now. getYear (); // current year
  6. NowYear + = (nowYear <2000 )? 1900: 0 ;//
  7. // Format the date: yyyy-MM-dd
  8. Function formatDate (date ){
  9. Var myyear = date. getFullYear ();
  10. Var mymonth = date. getMonth () + 1;
  11. Var myweekday = date. getDate ();
  12. If (mymonth <10 ){
  13. Mymonth = "0" + mymonth;
  14. }
  15. If (myweekday <10 ){
  16. Myweekday = "0" + myweekday;
  17. }
  18. Return (myyear + "-" + mymonth + "-" + myweekday );
  19. }
  20. // Obtain the number of days in a month
  21. Function getMonthDays (myMonth ){
  22. Var monthStartDate = new Date (nowYear, myMonth, 1 );
  23. Var monthEndDate = new Date (nowYear, myMonth + 1, 1 );
  24. Var days = (monthEndDate-monthStartDate)/(1000*60*60*24 );
  25. Return days;
  26. }
  27. // Obtain the start month of the current quarter
  28. Function getQuarterStartMonth (){
  29. Var quarterStartMonth = 0;
  30. If (nowMonth <3 ){
  31. QuarterStartMonth = 0;
  32. }
  33. If (2 <nowMonth & nowMonth <6 ){
  34. QuarterStartMonth = 3;
  35. }
  36. If (5 <nowMonth & nowMonth <9 ){
  37. QuarterStartMonth = 6;
  38. }
  39. If (nowMonth> 8 ){
  40. QuarterStartMonth = 9;
  41. }
  42. Return quarterStartMonth;
  43. }
  44. // Obtain the start date of the week
  45. Function getWeekStartDate (){
  46. Var weekStartDate = new Date (nowYear, nowMonth, nowDay-nowDayOfWeek );
  47. Return formatDate (weekStartDate );
  48. }
  49. // Obtain the end date of the week
  50. Function getWeekEndDate (){
  51. Var weekEndDate = new Date (nowYear, nowMonth, nowDay + (6-nowDayOfWeek ));
  52. Return formatDate (weekEndDate );
  53. }
  54. // Obtain the start date of this month
  55. Function getMonthStartDate (){
  56. Var monthStartDate = new Date (nowYear, nowMonth, 1 );
  57. Return formatDate (monthStartDate );
  58. }
  59. // Obtain the end date of this month
  60. Function getMonthEndDate (){
  61. Var monthEndDate = new Date (nowYear, nowMonth, getMonthDays (nowMonth ));
  62. Return formatDate (monthEndDate );
  63. }
  64. // Obtain the start date of the current quarter
  65. Function getQuarterStartDate (){
  66. Var quarterStartDate = new Date (nowYear, getQuarterStartMonth (), 1 );
  67. Return formatDate (quarterStartDate );
  68. }
  69. // Or end date of the current quarter
  70. Function getQuarterEndDate (){
  71. Var quarterEndMonth = getQuarterStartMonth () + 2;
  72. Var quarterStartDate = new Date (nowYear, quarterEndMonth, getMonthDays (quarterEndMonth ));
  73. Return formatDate (quarterStartDate );
  74. }

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.