Javascript Javascript以格式获取日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4929258/
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-08-23 14:49:04  来源:igfitidea点击:

Javascript get date in format

javascriptdate

提问by Samuel Meddows

I want to get today's date in the format of mm-dd-yyyy

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

I am using var currentDate = new Date(); document.write(currentDate);

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

I can't figure out how to format it.

我不知道如何格式化它。

I saw the examples var currentTime = new Date(YY, mm, dd);and currentTime.format("mm/dd/YY");

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

Both of which don't work

两者都不起作用

I finally got a properly formatted date using

我终于得到了一个格式正确的日期使用

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

This seems very complex for such a simple task.

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

Is there a better way to get today's date in dd/mm/yyyy?

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

采纳答案by Ates Goral

Unfortunately there is no better way, but instead of reinventing the wheel, you could use a library to deal with parsing and formatting dates: Datejs

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

<plug class="shameless">

<plug class="shameless">

Or, if you find format specifiers ugly and hard to decipher, here's a concise formatting implementationthat allows you to use human-readable format specifiers (namely, the Date instance gettersthemselves):

或者,如果您发现格式说明符丑陋且难以破译,这里有一个简洁的格式实现,它允许您使用人类可读的格式说明符(即Date 实例 getter本身):

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

</plug>

</plug>

回答by mrded

var today = new Date();

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

回答by Victor

Simple answer is no. Thats the only way to do it that I know of. You can probably wrap into a function that you can reuse many times.

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

回答by Bao Le

date.jsis what you need. For example, snippet below is to convert a date to string as Java style

date.js正是你所需要的。例如,下面的代码片段是将日期转换为字符串作为 Java 样式

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

回答by Widada

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

format this function is mm dd yyyy and the dividing you can choice and replace if you want... for example dateNow("/") you will get 12/12/2014

格式此函数为 mm dd yyyy 并且您可以根据需要选择和替换分割...例如 dateNow("/") 您将获得 12/12/2014

回答by WantToDo

(new Date()).format("MM-dd-yyyy")

N.B. month is "MM" not "mm"

NB月份是“MM”而不是“mm”

回答by RichardTheKiwi

There is nothing built in, but consider using this if you are already using jQuery (and if not, then you should consider that as well!)

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

回答by Porthos

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