How to get 2 digit year w/ Javascript? [duplicate]

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));

Leave a Comment