daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Feb 14th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var today = new Date();
  2. var dd = today.getDate();
  3. var mm = today.getMonth()+1; //January is 0!
  4. var yyyy = today.getFullYear();
  5.  
  6. if (dd < 10) {
  7.     dd = '0'+dd
  8. }
  9.  
  10. if (mm < 10) {
  11.     mm = '0'+mm
  12. }
  13.  
  14. function add_x_months(old_date, num_months_to_add) {
  15.     var date_string_list = old_date.split("/");
  16.     console.log(date_string_list);
  17.     var current_year = parseInt(date_string_list[2]);
  18.     var current_date = parseInt(date_string_list[0]);
  19.     var current_month = parseInt(date_string_list[1]);
  20.     console.log("The current year is " + current_year);
  21.     console.log("The current month is " + current_month);
  22.     var num_months = parseInt(num_months_to_add);
  23.     var years_to_add = Math.floor((num_months + current_month - 1) / 12);
  24.     if (current_month == 2 && current_date == 29 && years_to_add > 0) {
  25.         if ((current_year + years_to_add) % 4 != 0) {
  26.             current_date = 28;
  27.         }
  28.     }
  29.     var new_year = current_year + years_to_add;
  30.     var new_date = current_date;
  31.     if (num_months % 12 == 0 && current_month == 12) {
  32.         var new_month = current_month;
  33.     }
  34.     else {
  35.         var new_month = (current_month + num_months) % 12;
  36.     }
  37.     var new_date_string = new_date + '/' + new_month + '/' + new_year;
  38.     return new_date_string;
  39. }
  40.  
  41. // today = dd + '/' + mm + '/' + yyyy;
  42. // console.log(today);
  43. today = '29/02/2017';
  44. console.log(today);
  45. var new_date = add_x_months(today, 24);
  46. console.log(new_date);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top