100

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

我发现的所有东西都使用 4 位数年份,我需要 2 位数。

4

5 回答 5

193

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

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

于 2013-06-25T20:31:32.557 回答
55

给定一个日期对象:

date.getFullYear().toString().substr(2,2);

它将数字作为字符串返回。如果您希望它为整数,只需将其包装在parseInt()函数中:

var twoDigitsYear = parseInt(date.getFullYear().toString().substr(2,2), 10);

以当前年份为一行的示例:

var twoDigitsCurrentYear = parseInt(new Date().getFullYear().toString().substr(2,2));
于 2013-06-25T20:25:10.510 回答
14
var d = new Date();
var n = d.getFullYear();

是的,n 会给你 4 位数的年份,但你总是可以使用 substring 或类似的东西来分割年份,因此只给你两位数:

var final = n.toString().substring(2);

这将为您提供年份的最后两位数字(2013 年将变为 13,等等...)

如果有更好的方法,希望有人发布!这是我能想到的唯一方法。让我们知道它是否有效!

于 2013-06-25T20:26:02.060 回答
10
var currentYear =  (new Date()).getFullYear();   
var twoLastDigits = currentYear%100;

var formatedTwoLastDigits = "";

if (twoLastDigits <10 ) {
    formatedTwoLastDigits = "0" + twoLastDigits;
} else {
    formatedTwoLastDigits = "" + twoLastDigits;
}
于 2013-06-25T20:27:40.490 回答
7

另一个版本:

var yy = (new Date().getFullYear()+'').slice(-2);
于 2013-06-25T20:30:41.163 回答