Revisions
-
HT-7 revised this gist
on Jun 29, 2022Jun 29, 2022 . 1 changed file with 26 additions and 4 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,11 @@ var calcDate_data = {}; // initializing data object function calcDate(date1, date2){ // memorize input to detect invalid input calcDate_data.input1 = date1; calcDate_data.input2 = date2; // initiate date object var dt_date1 = new Date(date1); var dt_date2 = new Date(date2); @@ -41,10 +47,26 @@ if (months_passed > 1 ) result+=months_passed+' ' + months_plural[1]+' '; if (days_passed == 1) result+=days_passed+' ' + days_plural[0]; if (days_passed > 1 ) result+=days_passed+' ' + days_plural[1]; // put last result into object so it can be alerted to the user calcDate_data.last_result = { "total_days" : Math.round(total_days), "text" : result }; // message returned when an invalid date was input if (! calcDate_data.last_result.text || calcDate_data.input1==null || calcDate_data.input2==null) { calcDate_data.last_result.text = "At least one input date is invalid."; } // return the result return calcDate_data.last_result; } // example use calcDate( // prompt user to enter two dates prompt("Date calculator \n Enter start date"), prompt("Date calculator \n Enter end date") ); alert(calcDate_data.last_result.text); // alert result text -
HT-7 created this gist
on Jun 23, 2022Jun 23, 2022 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ function calcDate(date1, date2){ // initiate date object var dt_date1 = new Date(date1); var dt_date2 = new Date(date2); // get the time stamp date1 = dt_date1.getTime(); date2 = dt_date2.getTime(); var calc; // check which time stamp is greater if (date1 > date2){ calc = new Date(date1 - date2) ; } else { calc = new Date(date2 - date1) ; } // retrieve the date, month and year var calc_format_tmp = calc.getDate() + '-' + (calc.getMonth()+1)+ '-'+calc.getFullYear(); // convert to an array and store var calc_format = calc_format_tmp.split("-"); // subtract each member of our array from the default date var days_passed = parseInt(Math.abs(calc_format[0]) - 1); var months_passed = parseInt(Math.abs(calc_format[1]) - 1); var years_passed = parseInt(Math.abs(calc_format[2] - 1970)); // set up custom text var years_plural = ["year", "years"]; var months_plural = ["month", "months"]; var days_plural = ["day", "days"]; // convert to days and sum together var total_days = (years_passed * 365) + (months_passed * 30.417) + days_passed; // display result with custom text var result = ""; // declare string if (years_passed == 1) result+=years_passed+' ' + years_plural[0]+' '; if (years_passed > 1 ) result+=years_passed+' ' + years_plural[1]+' '; if (months_passed == 1) result+=months_passed+' ' + months_plural[0]+' '; if (months_passed > 1 ) result+=months_passed+' ' + months_plural[1]+' '; if (days_passed == 1) result+=days_passed+' ' + days_plural[0]; if (days_passed > 1 ) result+=days_passed+' ' + days_plural[1]; // return the result return { "total_days" : Math.round(total_days), "result" : result }; }