Hello,
I need some assistance.
I have two date pickers.
DatePicker1 allows the user to select the day he/she 1st opened their credit card account.
DatePicker2 is disabled and set to "today's date" by default.
I would like to calculate how long they have had the account opened in Years,Months, Days.
for example:
If the user selected the Account Open datepicker1 as: January 1 2015
and the hidden "Today's" datepicker2 is: April 10 2019
I would like to subtract the account date by today's date for a result of:
4 years 3 months 9 days
I am willing to settle for just the Years & Months difference if the days is too complicated.
Here is my desperate attempt at figuring out a code: ( That does not work, of course )
$w.onReady(function () { getDates(); }); export function datePicker1_change(event, $w) { let date1 = $w("#datePicker1").value; getDates(); let date2 = $w("#datePicker2").value; getDates(); } export function datePicker2_viewportEnter(event) { let date2 = $w("#datePicker2").value; getDates(); }
export function date_diff() {
let dt1 = new Date($w("#datePicker1").value);
let dt2 = new Date($w("#datePicker2").value);
return Math.floor((Date.UTC(dt2.getFullYear()) - Date.UTC(dt1.getFullYear()) ));
}
function getDates(){
let diff = date_diff();
const dates = (Number(diff));
$w("#test1").text = String(dates);
}
Just like the example below, but just Years, Months, Days:
Please HELP!
PS: not to sound ungrateful, but....... If you plan on putting a link to reference pages or javascript training school, i'm going to self-destruct and implode :-)
I have been greatly helped by wix experts who actually care enough to take the time to provide a example code specific to my needs. But some of you take the lazy way out. ( most users scour the exact links you resend us for months before actually reaching out to a forum ) so imagine the pure disappointment when a "expert" pretty much says
" I dunno, figure it out. here https://www.wix.com/corvid/reference/$w.DatePicker.html "
But i do appreciate some actual guidance and useful information to get me in the right direction!
FOUND A SOLUTION!!
You will need 6 total user input fields to calculate within.
1. Current Month
2. Current Year
3. Year if less or equal ( if the user inputs a month less than or equal to current month)
4. Month if less or equal ( same as above)
5. Year Greater than ( if user selects a month greater than current month)
ex: current month is april, if user selects may,june,july etc the code will calculate differently
6. Month Greater Than (Same as above)
Code:
$w.onReady(function () { $w("#Preloader").expand(); const millisecondsToDelay = 6000; setTimeout(() => { $w("#Preloader").collapse();
}, millisecondsToDelay);
// The current month is pulled // const today = new Date(); const options = { month: "numeric", };
$w("#CurrentMonth").value = today.toLocaleDateString("en-US", options);
// the current year is set //
var year = 2019; $w('#CurrentYear').value = "2019"; }); export function SaveA1_click(event) { $w('#SaveA1').collapse(); $w('#A1Continue').expand(); $w('#A1Month').value = (Number($w('#A1Month').value)); $w('#YearDiff').show(); var UseCheck = $w('#TypeA1').value; var MonthCheck = (Number($w('#CurrentMonth').value)); var AccountMonth= (Number($w('#A1Month').value)) var MonthDiff = (Number($w('#A1Month').value) + (Number($w('#CurrentMonth').value))); var MonthFix = 12 - (Number($w('#A1Month').value)) var NewMonth = MonthFix + MonthCheck; $w('#YearResult').value = (Number($w('#CurrentYear').value - (Number($w('#A1Year').value)))); var NewYearResult = (Number($w('#YearResult').value) - 1); var NewMonthResult = NewMonth;
if (UseCheck === "1"){ $w('#UseA1').expand(); } else if (UseCheck === "2"){ $w('#UseA1').collapse(); } else if (UseCheck === "3"){ $w('#UseA1').collapse(); }
if (MonthCheck < AccountMonth){ $w('#MonthResult').value = (Number($w('#CurrentMonth').value - (Number($w('#A1Month').value)))); $w('#YearResult').value = (Number($w('#CurrentYear').value - (Number($w('#A1Year').value)))); $w('#input4').value = NewYearResult; $w('#input5').value = NewMonthResult; $w('#YearDiff').text = "This account has an age of: " + $w('#input4').value + " yrs " + "& " + $w('#input5').value + " months"; $w('#pushA1Age').value = (Number($w('#input4').value) + "." + (Number($w('#input5').value))); } else if (MonthCheck >= AccountMonth){ $w('#YearResult').value = (Number($w('#CurrentYear').value - (Number($w('#A1Year').value)))); $w('#MonthResult').value = (Number($w('#CurrentMonth').value - (Number($w('#A1Month').value)))); $w('#YearDiff').text = "This account has an age of: " + $w('#YearResult').value + " yrs " + "& " + $w('#MonthResult').value + " months"; $w('#pushA1Age').value = (Number($w('#YearResult').value) + "." + (Number($w('#MonthResult').value))); }
}