Hey guys,
JavaScript has a bunch of functions for controlling how dates are displayed. Below I used “toLocaleDateString()”, which formats a date as a string based on your settings, including the language. You can read more about it here .
$w.onReady(function () {
// Get the date from the date field of the current item
const date = $w("#myDataset").getCurrentItem().date;
// Define the date preferences
const options = {
weekday: "short",
year: "numeric",
month: "long",
day: "numeric"
};
// Display the date in the text element using the defined preferences
$w("#text1").text = date.toLocaleDateString("en-US", options);
});
Edgar, I cannot claim to understand how this works 100% but it does work.
Thanks for posting - this is a real help!
Hi, I use code from Edgar Koster (//Field Two) and its work, thank you… but i want add code, when the date is not set in the database, it does not show anything … Any tips??
just wanted to say thanks to Edgar and everyone for this thread. This issue was driving me crazy.
Can anyone assist me? For some reason when I placed the code in, I get an error in the closing marks.
Please let me know what line 26 should be changed to. Thanks!
Hi @sharond-wix ,
Thank you for the code!
I have another question. I have a database full of events, at diffrent time and place. I am using a repeater to display it. I want to filter the dataset depending on event name, place, and date. At this stage, I have a code for filtering the event according to its name and place. However, every time I change the place, I loose the information on the date. I get the same date displayed for every event.
Is there any sequencing I have to do to avoid this?
Great post, very helpful. What would I add to show the AM or PM here here is what I have so far
$w.onReady( **function** () {
$w("#repeater1").onItemReady( ($w, itemData, index) => {
const monthNames = [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”];
console.log(itemData.dateSubmitted);
console.log(itemData.dateSubmitted.getDate().toString());
console.log(monthNames[itemData.dateSubmitted.getMonth()]);
console.log(itemData.dateSubmitted.getFullYear().toString());
console.log(itemData.dateSubmitted.getHours().toString());
console.log(itemData.dateSubmitted.getMinutes().toString());
const strDate = itemData.dateSubmitted.getDate().toString();
const strMonth = monthNames[itemData.dateSubmitted.getMonth()];
const strYear = itemData.dateSubmitted.getFullYear().toString();
const strHour = (“0” + itemData.dateSubmitted.getHours().toString()).slice(-2);
const strMinute = (“0” + itemData.dateSubmitted.getMinutes().toString()).slice(-2);
$w("#text43").text = strMonth + " " + strDate + " " + strYear + " | " + strHour ;
});
});
it is not working at all. I see dataset US format only.