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

IT技术 javascript date
2021-02-25 22:00:09

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

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

5个回答

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

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

如果您要使用单行...月 = ("0" + (d.getMonth() + 1).toString()).substr(-2)
2021-04-21 22:00:09
var day = d.getDate().toString().padStart(2,"0"); 不需要 if 语句...
2021-04-23 22:00:09
@Merovex 或只是 console.log(("0" + (new Date().getMonth() + 1).toString()).substr(-2) + ("0" + new Date().getDate().toString()).substr(-2) + new Date().getFullYear().toString().substr(-2))
2021-04-25 22:00:09
@Salketer 真的很酷,IE 不支持 padStart 所以它不是像上面的函数那样的全局解决方案。此外,我确定 padStart 正在执行一个循环,并且可能类似于 if 语句,因此从代码复杂性的角度来看,您不会获得太多收益。
2021-05-04 22:00:09
投反对票,因为在 10.000 (jk xD) 之后将无法工作,顺便说一句,使用.slice(-2).
2021-05-13 22:00:09

给定一个日期对象:

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));
您指定方法的第二个参数的目的是什么substr()使用起来不是更简单date.getFullYear().toString().substr(2);吗?
2021-05-18 22:00:09
@Alexander Abakumov 你是对的,当你使用不超过四位数的年份时 ;) 这来自我的防御性编码风格。
2021-05-19 22:00:09
var d = new Date();
var n = d.getFullYear();

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

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

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

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

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

var formatedTwoLastDigits = "";

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

另一个版本:

var yy = (new Date().getFullYear()+'').slice(-2);
为什么不使用 toString() 而不是添加一个空字符串? var yy = ((new Date().getFullYear().toString()).slice(-2);
2021-05-04 22:00:09
个人习惯,空/未定义的不同行为以及性能
2021-05-08 22:00:09