Hi
Just wanted to know the code on how to find the difference in days and storing it in a input field.
For example, From Date and To Date(OnBlur) the variable should be the difference between the two dates and storing in the Input field
Please help me out
Grab the datepicker’s value (which is a Date object), use getTime() to get value in epoch time and subtract.
let date1 = $w("#datePicker1").value
let date2 = $w("#datePicker2").value
let timeDiff = date2.getTime() - date1.getTime()
this gives you the diff in milliseconds.
now if you want to convert to days for example, just divide the value you got in ms:
let diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
It worked perfect. Thanks Chief
Hello! The example code in this thread looks pretty straight forward, but my 1st line (Line 6) of code returns a “null” instead of a date value.
3 $w.onReady( function () {
4 //TODO: write your page related code here…
5
6 let purchDate = $w(“#datePicker2”).value;
7 let todaysDate = new Date();
8
9 let timeDiff = purchDate.getTime() + todaysDate.getTime();
10
11 let timeConv = Math.ceil(timeDiff / (1000360024));
12
13 console.log (purchDate)
I must be missing something.!? Does someone have an idea?
Did you get this working yet? I need to do the same thing…
Hi cwvega76!
Yes. I did get it to work. Here is the final, working code for my application.
(If an item is 395 days past the purchase date, my warranty text element displays “EXPIRED” else, it shows days remaining.)
NOTE: Make sure your collection has a value in the field the Date Picker refers to (#datePicker2). Mine didn’t, so it kept coming up with a “null” error. Once I added a date in the collection record, it worked fine. Imagine that! I will eventually add an .onClick bit of code so it updates as soon as the Purchase Date element is changed, rather than it relying solely on a preexisting value in the collection.
// For full API documentation, including code examples, visit http://wix.to/94BuAAs
$w.onReady( function () {
//TODO: write your page related code here…
$w("#dynamicDataset").onReady( () => { // Allow dataset to load;
let purchDate = $w(“#datePicker2”).value;
let todaysDate = new Date();
let timeDiff = todaysDate.getTime() - purchDate.getTime();
let timeConv = Math.ceil(timeDiff / (1000360024));
$w("#dataset1").onReady( () => { // Allow dataset to load;
if (timeConv > 395) {
$w("#text110").text = "EXPIRED"
}
else
{
let daysLeft = 395 - timeConv;
$w(“#text110”).text = daysLeft.toString();
}
});
Hope that helps.