115

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.

Ian
  • 50,146
  • 13
  • 101
  • 111
Brandon
  • 1,175
  • 2
  • 7
  • 5

5 Answers5

209

The specific answer to this question is found in this one line below:

//pull the last two digits of the year
//logs to console
//creates a new date object (has the current date and time by default)
//gets the full year from the date object (currently 2017)
//converts the variable to a string
//gets the substring backwards by 2 characters (last two characters)    
console.log(new Date().getFullYear().toString().substr(-2));

Formatting Full Date Time Example, Single Function (MMddyy):

JavaScript:

//A function for formatting a date to MMddyy
function formatDate(d)
{
    //get the month
    var month = d.getMonth();
    //get the day
    //convert day to string
    var day = d.getDate().toString().padStart(2, '0');
    //get the year
    var year = d.getFullYear();
    
    //pull the last two digits of the year
    year = year.toString().substr(-2);
    
    //increment month by 1 since it is 0 indexed
    //converts month to a string
    month = (month + 1).toString().padStart(2, '0');

    //return the string "MMddyy"
    return month + day + year;
}

var d = new Date();
console.log(formatDate(d));

Formatting Full Date Example, multiple functions (MMddyy):

// function getMonth with 1 parameter expecting date
// This function returns a string of type MM (example: 05 = May)
function getMonth(d) {
    //get the month
    var month = d.getMonth();
    
    //increment month by 1 since it is 0 indexed
    //converts month to a string
    //if month is 1-9 pad right with a 0 for two digits
    month = (month + 1).toString().padStart(2, '0');
    
    return month;
}

// function getDay with 1 parameter expecting date
// This function returns a string of type dd (example: 09 = The 9th day of the month)
function getDay(d) {
    //get the day
    //convert day to string
    //if day is between 1-9 pad right with a 0 for two digits
    var day = d.getDate().toString().padStart(2, '0');;
    
    return day;
}

// function getYear with 1 parameter expecting date
// This function returns the year in format yy (example: 21 = 2021)
function getYear(d) {
    //get the year
    var year = d.getFullYear();
    
    //pull the last two digits of the year
    year = year.toString().substr(-2);
    
    return year;
}

//A function for formatting a date to MMddyy
function formatDate(d)
{
    //return the string "MMddyy"
    return getMonth(d) + getDay(d) + getYear(d);
}

var d = new Date();
console.log(formatDate(d));

Answers for full browser support (Internet Explorer)

Formatting Full Date Time Example, Single Function (MMddyy): jsFiddle

JavaScript:

//A function for formatting a date to MMddyy
function formatDate(d)
{
    //get the month
    var month = d.getMonth();
    //get the day
    //convert day to string
    var day = d.getDate().toString();
    //get the year
    var year = d.getFullYear();
    
    //pull the last two digits of the year
    year = year.toString().substr(-2);
    
    //increment month by 1 since it is 0 indexed
    //converts month to a string
    month = (month + 1).toString();

    //if month is 1-9 pad right with a 0 for two digits
    if (month.length === 1)
    {
        month = "0" + month;
    }

    //if day is between 1-9 pad right with a 0 for two digits
    if (day.length === 1)
    {
        day = "0" + day;
    }

    //return the string "MMddyy"
    return month + day + year;
}

var d = new Date();
console.log(formatDate(d));

Formatting Full Date Example, multiple functions (MMddyy):

// function getMonth with 1 parameter expecting date
// This function returns a string of type MM (example: 05 = May)
function getMonth(d) {
    //get the month
    var month = d.getMonth();
    
    //increment month by 1 since it is 0 indexed
    //converts month to a string
    month = (month + 1).toString();

    //if month is 1-9 pad right with a 0 for two digits
    if (month.length === 1)
    {
        month = "0" + month;
    }
    
    return month;
}

// function getDay with 1 parameter expecting date
// This function returns a string of type dd (example: 09 = The 9th day of the month)
function getDay(d) {
    //get the day
    //convert day to string
    var day = d.getDate().toString();
    
      //if day is between 1-9 pad right with a 0 for two digits
    if (day.length === 1)
    {
        day = "0" + day;
    }
    
    return day;
}

// function getYear with 1 parameter expecting date
// This function returns the year in format yy (example: 21 = 2021)
function getYear(d) {
    //get the year
    var year = d.getFullYear();
    
    //pull the last two digits of the year
    year = year.toString().substr(-2);
    
    return year;
}

//A function for formatting a date to MMddyy
function formatDate(d)
{
    //return the string "MMddyy"
    return getMonth(d) + getDay(d) + getYear(d);
}

var d = new Date();
console.log(formatDate(d));
abc123
  • 17,855
  • 7
  • 52
  • 82
  • 8
    Downvote because won't work after year 10.000 (jk xD) Btw, easily fixed with `.slice(-2)`. – Jorge Fuentes González Mar 21 '17 at 23:11
  • If you are going for oneliners... month = ("0" + (d.getMonth() + 1).toString()).substr(-2) – Merovex Dec 29 '19 at 14:22
  • @Merovex or just `console.log(("0" + (new Date().getMonth() + 1).toString()).substr(-2) + ("0" + new Date().getDate().toString()).substr(-2) + new Date().getFullYear().toString().substr(-2))` – abc123 Dec 30 '19 at 06:25
  • 1
    `var day = d.getDate().toString().padStart(2,"0");` no need for if statements... – Salketer Aug 24 '20 at 14:52
  • @Salketer that's really cool, IE doesn't support padStart so it isn't a global solution like the function above. Also i'm sure padStart is doing a loop and probably something like an if statement so from a code complexity standpoint you aren't gaining much i'd wager. – abc123 Sep 09 '20 at 20:21
  • Interesting... About complexity, padStart is 10 times less complex than a for loop. It is way easier to use and read. I'm sure you meant performance wise, then I'd assume it would be pretty much the same yes – Salketer Sep 11 '20 at 08:52
  • @Salketer correct i was describing Cyclomatic complexity, it probably is easier to read. – abc123 Sep 15 '20 at 15:28
  • 3
    `slice(-2)` should be used even if you don't care about supporting >10000 dates as `.substr()` is deprecated – yeah22 Jan 08 '22 at 01:35
57

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));
Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
Martin Lantzsch
  • 1,851
  • 15
  • 19
  • 3
    For what purpose are you specifying the second parameter of the `substr()` method? Wouldn't it be simplier to use `date.getFullYear().toString().substr(2);`? – Alexander Abakumov Sep 07 '15 at 14:52
  • @Alexander Abakumov You are right, when you are working with years which don't exceed four digits ;) This comes from my defensive coding style. – Martin Lantzsch Sep 08 '15 at 08:37
17
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!

Ricky Mutschlechner
  • 4,291
  • 2
  • 31
  • 36
12
var currentYear =  (new Date()).getFullYear();   
var twoLastDigits = currentYear%100;

var formatedTwoLastDigits = "";

if (twoLastDigits <10 ) {
    formatedTwoLastDigits = "0" + twoLastDigits;
} else {
    formatedTwoLastDigits = "" + twoLastDigits;
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
8

another version:

var yy = (new Date().getFullYear()+'').slice(-2);
Guido Preite
  • 14,905
  • 4
  • 36
  • 65