如何使用 Javascript 获得 2 位数的年份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17306830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:49:12  来源:igfitidea点击:

How to get 2 digit year w/ Javascript?

javascriptdate

提问by Brandon

I am trying to find some javascript code that will write the current date in this format: mmddyy

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

Everything I have found uses 4 digit years and I need 2 digit.

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

回答by abc123

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 (MMddyy): jsFiddle

格式化完整日期时间示例 (MMddyy):jsFiddle

JavaScript:

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

回答by Martin Lantzsch

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:

它将数字作为字符串返回。如果你想要它作为整数,只需将它包装在parseInt()函数中:

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

回答by Ricky Mutschlechner

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:

是的, n 会给你 4 位数字的年份,但你总是可以使用子字符串或类似的东西来分割年份,从而只给你两位数字:

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

This will give you the last two digits of the year (2013 will become 13, etc...)

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

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!

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

回答by VirtualTroll

var currentYear =  (new Date()).getFullYear();   
var twoLastDigits = currentYear%100;

var formatedTwoLastDigits = "";

if (twoLastDigits <10 ) {
    formatedTwoLastDigits = "0" + twoLastDigits;
} else {
    formatedTwoLastDigits = "" + twoLastDigits;
}

回答by Guido Preite

another version:

另一个版本:

var yy = (new Date().getFullYear()+'').slice(-2);