0

I'm using Elian Ebbing's data validation code from here and after validation, I'd like to take the date entered and return a new date for X amount of months later. For example, if I entered 06/09/2019, I would then like the code to return the correct new date that's 6 months later, which would be 12/6/2019.

Can someone please help guide me through the process of accomplishing this? I have been trying different methods of reusing the original code to get the results that I want, however I have been at this since July 2nd and have concluded I just can't figure this out on my own. I am completely stumped.

Lastly, my deepest apologies in advance that I didn't just comment on the original thread for Mr. Ebbing's code and ask for help, but unfortunately I did not have enough reputation points to do so.

Rodney
  • 1
  • 5
  • Use moment.js library ;) – Iiskaandar Jul 31 '19 at 14:13
  • 2
    Which date system are you using, where "06/09/2019 + 6 months = 12/6/2019"? – Niet the Dark Absol Jul 31 '19 at 14:14
  • Yes, use momentjs: https://jsfiddle.net/khrismuc/qcua3hdo/ –  Jul 31 '19 at 14:23
  • Seems like you have already found a solution for the validation piece and are just looking for how to add months to your date. If that is right, then look into https://stackoverflow.com/questions/5645058/how-to-add-months-to-a-date-in-javascript or https://stackoverflow.com/questions/2706125/javascript-function-to-add-x-months-to-a-date – benvc Jul 31 '19 at 14:29
  • Thanks for the moment.js suggestion. I did come across many threads on it as a possible solution during my search. However, since I also want to learn how to write code, I opted not to take that route. Lastly, I should have added that I am using this validation code in Google App Script to automate a process for a document I constantly use. Anyway, thanks again to everyone who chimed and I will check out the links shared with me. – Rodney Aug 01 '19 at 14:35

1 Answers1

1

If you are not sure that it is good to use some library (moment.js). If you want to find something already discovered, be ready to bump your head.

 // Elian Ebbing validator

function isValidDate(dateString) {
        // First check for the pattern
        if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
        return false;

        // Parse the date parts to integers
        var parts = dateString.split("/");
        var day = parseInt(parts[1], 10);
        var month = parseInt(parts[0], 10);
        var year = parseInt(parts[2], 10);

        // Check the ranges of month and year
        if(year < 1000 || year > 3000 || month == 0 || month > 12)
        return false;

        var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

        // Adjust for leap years
        if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        monthLength[1] = 29;

        // Check the range of the day
        return day > 0 && day <= monthLength[month - 1];
}

// if you want to change date format

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1), // monts start form 0 so for result 06/01/2019
        day = '' + d.getDate(),
        year = d.getFullYear();
    if (month.length < 2) {
        month = '0' + month;
    }      
    if (day.length < 2) {
         day = '0' + day;
    }
    return [month, day, year].join('/');
}

// increment Date with count of months

function incrementDate(date, counter = 0) {
    if (isValidDate(start_date_value)) { 
        var newDate  = new Date(date);
        newDate.setMonth(newDate.getMonth() + counter);
        console.log(formatDate(newDate));
    }
}

var start_date_value = "01/01/2019";
incrementDate(start_date_value, 5) ;  // 06/01/2019
Ivan Ganchev
  • 174
  • 2
  • 10
  • Thanks Ivan! Woah, did I completely miss these functions during my search? I'll give them a test shortly to see if it resolves my issue. Gratitude! – Rodney Aug 01 '19 at 14:41
  • I would be glad if I helped @Rodney, but don't overlook the tips for using something proven (like libraries). – Ivan Ganchev Aug 01 '19 at 16:11
  • I finally got around to finishing up my project today, so I wanted to make sure that I loop back to this thread and say thank you to everyone who pitched in. With all of your help, I was able to get the exact outcome I was looking for. I know I could have used a library to easily resolve my issue, but I sincerely want to get better at writing and understanding the ins and outs of Javascript. With that said, I felt that taking the long and tough route did help make a beginner like me a bit more knowledgeable towards the language. And for that, I am very grateful. Thanks again everyone! – Rodney Aug 14 '19 at 20:35