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 >
<p>Offer expires on <span id="date"></span></p>
</div>
ar d = new Date();
d.setDate(d.getDate() 10);
document.getElementById("date").innerHTML = d ;
CodePudding user response:
Try this
var today = new Date();
var dd = String(today.getDate() 5).padStart(2,'0'); // The 5 adds 5 days to the current date
var mm = String(today.getMonth() 1).padStart(2,'0');
var yyyy = today.getFullYear();
today = dd '/' mm '/' yyyy;
console.log(today);
CodePudding user response:
date.toISOString().slice(0, 10):
Convert date to string and get first 10 character.
CodePudding user response:
This should work if the days overflow with the months.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add Days to Date</title>
</head>
<body>
<div >
<p>Offer expires on <span id="date"></span></p>
</div>
<script>
//Date class
var d = new Date();
//Returns the current day
var day = d.getDate();
//Returns the current month
var month = d.getMonth();
//Returns the current year
var year = d.getFullYear();
//New Date Class for the current date plus added days
//Days overflowing will make a new month and a new year if needed
//see "https://www.w3schools.com/js/js_dates.asp" for more info
var newDate = new Date(year, month, day 5);
//month indexes are 0-11 for Jan-Dec, so the added one is necessary
var dd = newDate.getDate();
var mm = newDate.getMonth() 1;
var yyyy = newDate.getFullYear();
//for the double digit string
dd = dd.toString().padStart(2,"0");
mm = mm.toString().padStart(2,"0");
//Date string Message
var dateString = `${dd}/${mm}/${yyyy}`;
document.getElementById("date").innerHTML = dateString;
</script>
</body>
</html>