【问题标题】:Javascript get date in format [duplicate]Javascript以格式获取日期[重复]
【发布时间】:2011-06-23 04:33:44
【问题描述】:

我想以 mm-dd-yyyy 格式获取今天的日期

我正在使用var currentDate = new Date(); document.write(currentDate);

我不知道如何格式化它。

我看到了var currentTime = new Date(YY, mm, dd);currentTime.format("mm/dd/YY");的例子

这两个都不行

我终于用

得到了一个格式正确的日期
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!`

var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
var today = mm+'/'+dd+'/'+yyyy;
document.write(today);'`

对于这么简单的任务来说,这似乎非常复杂。

有没有更好的方法在dd/mm/yyyy 中获取今天的日期?

【问题讨论】:

  • 我会手动操作var d = new Date(); var s = new String(); s = (d.getHours()+"-"+d.getMinutes()+"-"+d.getSeconds()+" "+d.getDate()+"-"+d.getMonth()+"-"+d.getFullYear()).toString();。参考:w3schools.com/jsref/jsref_obj_date.asp

标签: javascript date


【解决方案1】:
(new Date()).format("MM-dd-yyyy")

注意月份是“MM”而不是“mm”

【讨论】:

  • 您的描述不清楚。此外,这个问题已经有很多非常好的答案。你的回答有什么帮助吗?如果是这样,请详细说明您的答案。
【解决方案2】:
var today = new Date();

var strDate = 'Y-m-d'
  .replace('Y', today.getFullYear())
  .replace('m', today.getMonth()+1)
  .replace('d', today.getDate());

【讨论】:

    【解决方案3】:
    function dateNow(splinter){
      var set = new Date(); 
      var getDate = set.getDate().toString();
      if (getDate.length == 1){ //example if 1 change to 01
       getDate = "0"+getDate;
      }
      var getMonth = (set.getMonth()+1).toString();
      if (getMonth.length == 1){
       getMonth = "0"+getMonth;
      }
      var getYear = set.getFullYear().toString();
      var dateNow = getMonth +splinter+ getDate +splinter+ getYear; //today
      return dateNow;
    }
    

    这个函数的格式是 mm dd yyyy 以及您可以根据需要选择和替换的划分...例如 dateNow("/") 你会得到 12/12/2014

    【讨论】:

      【解决方案4】:
      function appendZeros(value,digits){
          var c= 1;
          initValue = value;
          for(i=0;i<digits-1;i++){
              c = c*10;
              if( initValue < c ){
                  value = '0' + value;
              }
          }
          return value;
      }
      

      【讨论】:

      • 也许这是为了另一个问题?
      【解决方案5】:

      date.js 是您所需要的。比如下面的sn-p就是把日期转成Java风格的字符串

      new Date().toString('M/d/yyyy')
      

      【讨论】:

        【解决方案6】:

        不幸的是,没有更好的方法,但您可以使用库来处理解析和格式化日期,而不是重新发明轮子:Datejs

        &lt;plug class="shameless"&gt;

        或者,如果您发现格式说明符丑陋且难以解读,here's a concise formatting implementation 允许您使用人类可读的格式说明符(即 the Date instance getters 本身):

        date.format("{Month:2}-{Date:2}-{FullYear}"); // mm-dd-yyyy
        

        &lt;/plug&gt;

        【讨论】:

        • 不错的库,派上用场了。
        • 另一个有用的javascript库是momentjs。 momentjs.com 您可以按如下方式格式化日期。 moment().format('MM-DD-YYYY')
        【解决方案7】:

        没有内置任何东西,但如果您已经在使用 jQuery,请考虑使用它(如果没有,那么您也应该考虑使用它!)

        【讨论】:

          【解决方案8】:

          简单的答案是否定的。这是我所知道的唯一方法。 您可能可以包装成一个可以重复使用多次的函数。

          【讨论】:

          • 认为可能是这样。谢谢老哥
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-05-29
          • 2018-05-11
          • 1970-01-01
          • 1970-01-01
          • 2018-04-15
          • 2014-04-24
          • 2013-03-10
          相关资源
          最近更新 更多