I need to create an expiration date and store it in a row in a collection using toUpdate. The next expiration date is today’s date with one added to the year, the first of the month and the same month. I want it to be stored so that it is later retrievable using
$w(“#txtexpDate”).text = items.expirationDate.toLocaleDateString(“en-US”, options);
which works now.
let expDate = new Date();
// Get new expiration date
expDate.setDate(1);
expDate.setFullYear(items.expirationDate.getFullYear() + 1); //one year later
expDate.setMonth(items.expirationDate.getMonth); //same month next year
$w(“#txtexpDate”).text = expDate.toLocaleDateString(“en-US”, options); // gives me invalid date
let toUpdate = {
“_id”: itemsID,
“membershipType”: $w(“#membershipType”).value,
“firstName”:$w(“#input1”).value,
“lastName”:$w(“#input2”).value,
“expirationDate”: expDate.text }; // doesn’t work
Would really appreciate the help… Thank you.
Amy,
I was intrigued to get this to work with the Javascript date set functions, but alas I fell back on an approach that I’ve implemented more than a few times. Of course, you will need to have an object in place that has the current record’s data in it that I’m calling item to get this written to the collection. Since it doesn’t seem like you are using a dataset on this page (probably better), you could use the get function or run a query to create that data object.
let expDate = new Date();
// Get new expiration date
let nextYear = expDate.getFullYear(expDate) + 1;
let month = expDate.getMonth()
let expDate2 = new Date(nextYear,month,1); //one year later
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
$w("#txtexpDate").text = expDate2.toLocaleDateString("en-US", options);
let toUpdate = {
"_id": item._id,
"membershipType": $w("#membershipType").value,
"firstName":$w("#input1").value,
"lastName":$w("#input2").value,
"expirationDate": expDate2 };
wixData.update('Collection',toUpdate)
.then((updateResult) => {
console.log(updateResult);
} )
.catch( (error) => {
console.log(error);
} );
Thank you so much AnthonyB. This worked perfectly.