I am trying to find some javascript code that will write the current date in this format: mmddyy
Everything I have found uses 4 digit years and I need 2 digit.
This question is related to
javascript
date
var d = new Date();
var n = d.getFullYear();
Yes, n will give you the 4 digit year, but you can always use substring or something similar to split up the year, thus giving you only two digits:
var final = n.toString().substring(2);
This will give you the last two digits of the year (2013 will become 13, etc...)
If there's a better way, hopefully someone posts it! This is the only way I can think of. Let us know if it works!
Given a date object:
date.getFullYear().toString().substr(2,2);
It returns the number as string. If you want it as integer just wrap it inside the parseInt() function:
var twoDigitsYear = parseInt(date.getFullYear().toString().substr(2,2), 10);
Example with the current year in one line:
var twoDigitsCurrentYear = parseInt(new Date().getFullYear().toString().substr(2,2));
var currentYear = (new Date()).getFullYear();
var twoLastDigits = currentYear%100;
var formatedTwoLastDigits = "";
if (twoLastDigits <10 ) {
formatedTwoLastDigits = "0" + twoLastDigits;
} else {
formatedTwoLastDigits = "" + twoLastDigits;
}
another version:
var yy = (new Date().getFullYear()+'').slice(-2);
Source: Stackoverflow.com