Wayne (and anyone else who needs it) ā¦ hereās what I ended up with (it works!!)
- Show a date from my database
- Calculate a date X days after the original start date, and show that on the page as well
- Format both dates to read as Month Date, Year (āApril 20, 2018ā)
Note: you can format the date any way you need by making a few changes to the line:
// formats date to Month Date, Year
var expDate = longMonth + " " + day + ", " + year;
Hereās the first part of the CODE ā
(This goes into data.js in the backend)
export function Members_afterQuery(item) {
item.expDate = setMemberExpDate(item._createdDate);
return item;
function setMemberExpDate(_createdDate) {
var originalDate = new Date(_createdDate);
var date = new Date(originalDate);
// set expiration 1 year
date.setDate(date.getDate() + 364);
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const day = date.getDate().toString();
const longMonth = monthNames[date.getMonth()];
const year = date.getFullYear().toString();
var expDate = longMonth + " " + day + ", " + year;
return expDate.toString();
}
}
THEN
(This goes into the page code ā in this case, Iām using static text fields on a dynamic page)
$w.onReady(function () {
$w(ā#membersDatasetā).onReady(() => {
populateCalculatedFields();
reformatJoinDate();
});
});
function populateCalculatedFields() {
const currentItem = $w(ā#membersDatasetā).getCurrentItem();
$w(ā#expDateā).text = currentItem.expDate;
}
function reformatJoinDate() {
let originalDate = $w(ā#joinDateā).text;
let date = new Date(originalDate);
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let day = date.getDate();
let longMonth = monthNames[date.getMonth()];
let year = date.getFullYear();
$w("#joinDate").text = longMonth + " " + day + ", " + year;
}
A few notesā¦
Iām sure the function reformatJoinDate can be simplified, as this is mostly a copy of the code in data.js ā but I kept getting stuck there, so this works
Also, if you are doing this in a repeater youāll need to add a line after the onReady
$w(" #repeater1 ").forEachItem( ($w) => {
So Wayne, youāll need to make a few changes to make this work for you:
- My expDate becomes your quoteDate
- Change the āMembersā in Members_afterQuery(item) to the name of your database
- If your original date doesnāt come from the _createdDate, change that as well
- Youāll need to make changes to the page code as well to connect to your page elements
Good luck, let me know how it goes!