在 JavaScript 中将日期格式化为 MM/dd/yyyy

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

Format date to MM/dd/yyyy in JavaScript

javascriptjquery

提问by Pradeep

I have a dateformat like this '2010-10-11T00:00:00+05:30'. I have to format in to MM/dd/yyyyusing JavaScript or jQuery . Anyone help me to do the same.

我有一个这样的日期格式'2010-10-11T00:00:00+05:30'。我必须格式化为MM/dd/yyyy使用 JavaScript 或 jQuery 。任何人都可以帮助我做同样的事情。

回答by yogi

Try this; bear in mind that JavaScript months are 0-indexed, whilst days are 1-indexed.

尝试这个; 请记住,JavaScript 月份是 0 索引的,而天是 1 索引的。

var date = new Date('2010-10-11T00:00:00+05:30');
    alert(((date.getMonth() > 8) ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1))) + '/' + ((date.getDate() > 9) ? date.getDate() : ('0' + date.getDate())) + '/' + date.getFullYear());

回答by Ore4444

All other answers don't quite solve the issue. They print the date formatted as mm/dd/yyyy but the question was regarding MM/dd/yyyy. Notice the subtle difference? MM indicates that a leading zero must pad the month if the month is a single digit, thus having it always be a double digit number.

所有其他答案都不能完全解决问题。他们打印格式为 mm/dd/yyyy 的日期,但问题是关于 MM/dd/yyyy。注意到细微的差别了吗?MM 表示如果月份是一位数,则必须用前导零填充月份,因此它始终是两位数。

i.e. whereas mm/dd would be 3/31, MM/dd would be 03/31.

即 mm/dd 为 3/31,MM/dd 为 03/31。

I've created a simple function to achieve this. Notice that the same padding is applied not only to the month but also to the day of the month, which in fact makes this MM/DD/yyyy:

我创建了一个简单的函数来实现这一点。请注意,相同的填充不仅适用于月份,还适用于月份中的第几天,这实际上使这个 MM/DD/yyyy:

function getFormattedDate(date) {
  var year = date.getFullYear();

  var month = (1 + date.getMonth()).toString();
  month = month.length > 1 ? month : '0' + month;

  var day = date.getDate().toString();
  day = day.length > 1 ? day : '0' + day;
  
  return month + '/' + day + '/' + year;
}



Update for ES2017 using String.padStart(), supported by all major browsers except IE.

使用 String.padStart() 更新 ES2017,除 IE 外的所有主要浏览器都支持。

function getFormattedDate(date) {
    let year = date.getFullYear();
    let month = (1 + date.getMonth()).toString().padStart(2, '0');
    let day = date.getDate().toString().padStart(2, '0');
  
    return month + '/' + day + '/' + year;
}

回答by Roko C. Buljan

Pass your string into the DateObject:

将您的字符串传递到DateObject 中

var d = new Date("2010-10-30T00:00:00+05:30");

from here you can extract the desired using the following methods:

从这里您可以使用以下方法提取所需的内容:

d.getMonth()+1  // 10
d.getDate()     // 30
d.getFullYear() // 2010

Note that getMonth()returns the month number zerobased (0-11)therefore a +1is needed.

请注意,getMonth()返回月份数基础(0-11)因此,一个+1是需要的。

Here you can find a list of other getters: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

在这里您可以找到其他getter的列表:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date



Another way, using .slice()and .split()

另一种方式,使用.slice().split()

var d = "2010-10-30T00:00:00+05:30".slice(0, 10).split('-');   
d[1] +'/'+ d[2] +'/'+ d[0]; // 10/30/2010