javascript - 如何使用 Javascript 获取 2 位数年份?

标签 javascript date

<分区>

我正试图找到一些将以这种格式写入当前日期的 javascript 代码:mmddyy

我发现的所有内容都使用 4 位数年份,而我需要 2 位数。

最佳答案

这个问题的具体答案可以在下面这一行中找到:

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

格式化完整日期时间示例,单一函数 (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));

格式化完整日期示例,多个函数 (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));

完整浏览器支持的答案 (Internet Explorer)

格式化完整日期时间示例,单一函数 (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));

格式化完整日期示例,多个函数 (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));

关于javascript - 如何使用 Javascript 获取 2 位数年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17306830/

相关文章:

javascript - 如果我不指定字符串,Angular 怎么知道要注入(inject)什么?

mysql - 填补月份空白

date - 使用 awk 或 sed 将时间戳(unix 13 位)转换为 csv 文件完整列的日期时间格式

javascript - .trigger ('click' ) 不适用于 mouseenter

javascript - 如何使用 CSS 动画创建无限符号轨迹?

javascript - $animate addClass 在 Angular 1.3 中不起作用

javascript - 在 Meteor 中设置 ShareJS

使用 lubridate 将持续时间或周期舍入到整分钟

使用 new Date() 将 Javascript 字符串转换为日期

Javascript编码: Input a specific date, 输出季节