All of those function return numbers, and what happens when you put a +
sign between numbers - it adds them.
Try turning them into strings first so they are concatenated, not added
i.e.
function myFunction() {
var dt = new Date();
alert(dt.getYear().toString() + (dt.getMonth() + 1).toString() + dt.getDate().toString() + dt.getSeconds().toString());
}
A couple of other points as per comments
.getYear()
is old and shouldn't really be used (see SO question here), it returns 114 for 2014, you should use .getFullYear()
, this will return 2014, but since you may only want a two digit year, trim the first two characters off
Something like:
function myFunction() {
var dt = new Date();
alert(dt.getYear().toString().substr(2) + (dt.getMonth() + 1).toString() + dt.getDate().toString() + dt.getSeconds().toString());
}
Assuming you are tring to keep a set format of 2 digits for year, 2 digits for month, 2 digits for day and 2 digits for seconds, if any of these are below 10, you will need to pad with a zero. So I would make a helper function to pad,
Something like:
function pad(s, size) {
while (s.length < size) s = "0" + s;
return s;
}
then you can add it to your function
Something like:
function myFunction() {
var dt = new Date();
alert(pad(dt.getYear().toString().substr(2),2) + pad((dt.getMonth() + 1).toString(),2) + pad(dt.getDate().toString(),2) + pad(dt.getSeconds().toString(),2));
}