I’ve created this script to calculate the date for 10 days in advance in the format of dd/mm/yyyy:
var MyDate = new Date();
var MyDateString = new Date();
MyDate.setDate(MyDate.getDate()+10);
MyDateString = MyDate.getDate() + '/' + (MyDate.getMonth()+1) + '/' + MyDate.getFullYear();
I need to have the date appear with leading zeroes on the day and month component by way of adding these rules to the script. I can’t seem to get it to work.
if (MyDate.getMonth < 10)getMonth = '0' + getMonth;
and
if (MyDate.getDate <10)get.Date = '0' + getDate;
If someone could show me where to insert these into the script I would be really appreciative.
Try this: http://jsfiddle.net/xA5B7/
EDIT:
To explain,
.slice(-2)gives us the last two characters of the string.So no matter what, we can add
"0"to the day or month, and just ask for the last two since those are always the two we want.So if the
MyDate.getMonth()returns9, it will be:so adding
.slice(-2)on that gives us the last two characters which is:But if
MyDate.getMonth()returns10, it will be:so adding
.slice(-2)gives us the last two characters, or: