在JavaScript中格式化日期为MM/dd/yyyy[重复]

时间:2021-12-14 16:55:40

This question already has an answer here:

这个问题已经有了答案:

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

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

3 个解决方案

#1


201  

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() + 1) + '/' + date.getDate() + '/' +  date.getFullYear());

#2


103  

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/yyy。注意到细微的差异了吗?MM表示,如果月份是个位数,则前导0必须填充该月份,因此它总是一个双位数。

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

#3


23  

You can: .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


...or pass your string into the Date Object:

…或将字符串传递到日期对象:

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 zero based (0-11) therefore a +1 is 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

#1


201  

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() + 1) + '/' + date.getDate() + '/' +  date.getFullYear());

#2


103  

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/yyy。注意到细微的差异了吗?MM表示,如果月份是个位数,则前导0必须填充该月份,因此它总是一个双位数。

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

#3


23  

You can: .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


...or pass your string into the Date Object:

…或将字符串传递到日期对象:

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 zero based (0-11) therefore a +1 is 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