admin管理员组文章数量:1337123
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.
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.
- 19 How is this a duplicate, one guy asks where the documentation is, the other how to format a date into a specific format... ? – Joshua Robinson Commented Nov 27, 2016 at 14:09
-
2
not sure why everyone always adds strings together for this rather than use
toLocaleDateString
which is less jenky:function getDate(str) {var ops = {year: 'numeric'}; ops.month = ops.day = '2-digit'; return new Date(str).toLocaleDateString(0, ops);}
– omikes Commented Dec 21, 2019 at 0:04 -
@omikes I was not able to make your function work
var testGetDate = getDate('2010-10-11T00:00:00+05:30');
on ServiceNow platform. Not sure why, but it works on jsconsole. – Paul Hegel Commented Jun 26, 2020 at 16:20 -
3
The correct solution nowadays is to use the
Intl.DateTimeFormat
API – Wolfgang Kuehn Commented Mar 24, 2021 at 20:38 -
1
Yes, agreed, e.g.,
new Intl.DateTimeFormat('en-US').format(date)
- developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – chrismarx Commented May 25, 2023 at 18:34
3 Answers
Reset to default 316Try this; bear in mind that JavaScript months are 0-indexed, whilst days are 1-indexed.
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());
Some 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.
i.e. whereas mm/dd would be 3/31, MM/dd would be 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:
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.
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;
}
ISO pliant dateString
If the dateString is RFC282 and ISO8601 pliant:
pass the string into the Date Constructor.
Then, to format it into a desired date output use Intl.DateTimeFormat like:
const dateString = "2020-09-30T12:52:27+05:30"; // ISO8601 pliant dateString
const date = new Date(dateString); // {object Date}
const dateFormatted = new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit"
}).format(date);
console.log(dateFormatted); // "09/30/2020"
alternatively, we can extract the desired values by using Date Getters:
date.getMonth() + 1 // 9 (PS: use +1 since Month returns 0-based index)
date.getDate() // 30
date.getFullYear() // 2020
in the above example you might also want to use String.prototype.padStart in order to add the leading zeros for month and date, if needed.
Non-standard date string
If non-standard date string:
destructure the string into known parts, and then pass the variables to the Date Constructor:
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
const dateString = "30/09/2020 12:52:27"; // Not ISO8601 pliant
const [d, M, y, h, m, s] = dateString.match(/\d+/g);
// PS: M-1 since Month is 0-based
const date = new Date(Date.UTC(y, M-1, d, h, m, s)); // {object Date}
const dateFormatted = new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit"
}).format(date);
console.log(dateFormatted); // "09/30/2020"
本文标签: jqueryFormat date to MMddyyyy in JavaScriptStack Overflow
版权声明:本文标题:jquery - Format date to MMddyyyy in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742855153a2493502.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论