【问题标题】:How to get 2 digit year w/ Javascript? [duplicate]如何使用 Javascript 获得 2 位数年份? [复制]
【发布时间】:2023-03-16 06:22:01
【问题描述】:

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

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

【问题讨论】:

  • 不是子字符串的忠实粉丝是吗
  • 你是如何研究这个的?见stackoverflow.com/questions/1056728/…
  • 这是一个供您的曾孙子孙辈修复的问题new Date().getFullYear() - 2000
  • “我需要 2 位数”...真的吗?如果没有 2 位数的年份,你会死吗?相反,2 位数的年份可能会给您带来更多的痛苦。

标签: javascript date


【解决方案1】:

这个问题的具体答案在下面这一行:

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

【讨论】:

  • 投反对票,因为 10.000 (jk xD) 年之后将无法正常工作,顺便说一句,.slice(-2) 很容易修复。
  • 如果你要去oneliners...month = ("0" + (d.getMonth() + 1).toString()).substr(-2)
  • @Merovex 或只是console.log(("0" + (new Date().getMonth() + 1).toString()).substr(-2) + ("0" + new Date().getDate().toString()).substr(-2) + new Date().getFullYear().toString().substr(-2))
  • var day = d.getDate().toString().padStart(2,"0"); 不需要 if 语句...
  • @Salketer 真的很酷,IE 不支持 padStart 所以它不是像上面的函数那样的全局解决方案。此外,我确定 padStart 正在执行一个循环,并且可能类似于 if 语句,因此从代码复杂性的角度来看,您不会获得太多我愿意下注的东西。
【解决方案2】:

给定一个日期对象:

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);不是更简单吗?
  • @Alexander Abakumov 您是对的,当您使用不超过四位数的年份时;)这来自我的防御性编码风格。
【解决方案3】:
var d = new Date();
var n = d.getFullYear();

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

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

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

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

【讨论】:

    【解决方案4】:
    var currentYear =  (new Date()).getFullYear();   
    var twoLastDigits = currentYear%100;
    
    var formatedTwoLastDigits = "";
    
    if (twoLastDigits <10 ) {
        formatedTwoLastDigits = "0" + twoLastDigits;
    } else {
        formatedTwoLastDigits = "" + twoLastDigits;
    }
    

    【讨论】:

      【解决方案5】:

      另一个版本:

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

      【讨论】:

      • 为什么不使用 toString() 而不是添加一个空字符串? var yy = ((new Date().getFullYear().toString()).slice(-2);
      • 个人习惯,null/undefined 的不同行为以及性能
      猜你喜欢
      • 1970-01-01
      • 2013-07-30
      • 2020-03-24
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      相关资源
      最近更新 更多