so I got this function that adds 5 days to the current date, the only problem is that the date is displayed as "Mon May 30 2022 00:16:04 GMT+0300 (Eastern European Summer Time)" I need a simple, clean format like 22/07/2002.
<div class="container-date">
<p>Offer expires on <span id="date"></span></p>
</div>
ar d = new Date();
d.setDate(d.getDate() + 10);
document.getElementById("date").innerHTML = d ;
You can use formatDate(date, timeZone, format) method to easily format date objects. See this quick sample below:
function test() {
var d = new Date();
var formattedDate = Utilities.formatDate(new Date(d.setDate(d.getDate() + 5)), Session.getScriptTimeZone(), "dd/MM/yyyy")
console.log(formattedDate);
}
Try this
// Note this wont calculate 5days ahead ,it just gives the asked format!
var today = new Date();
var dd = String(today.getDate()).padStart(2,'0');
to the current date
var mm = String(today.getMonth()+1).padStart(2,'0');
var yyyy = today.getFullYear();
today = dd + '/' + mm + '/' + yyyy;
console.log(today);
date.toISOString().slice(0, 10): Convert date to string and get first 10 character.
toISOString() (2022-05-29T23:03:31.782Z to 2022-05-29)
date.split('-').reverse().join('/'): Split string by -, reverseit for formatting and convert array to a string with / separator. (2022-05-29 to 29/05/2022)
const addDays = (days) => {
let date = new Date();
date.setDate(date.getDate() + days);
date = date.toISOString().slice(0, 10);
return date.split('-').reverse().join('/');
}
const date = addDays(5);
console.log(date);