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.

- 59,771
- 29
- 135
- 184

- 4,612
- 10
- 38
- 50
-
15How is this a duplicate, one guy asks where the documentation is, the other how to format a date into a specific format... ? – Joshua Robinson Nov 27 '16 at 14:09
-
2not 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 Dec 21 '19 at 00: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.com – Paul Hegel Jun 26 '20 at 16:20
-
2The correct solution nowadays is to use the `Intl.DateTimeFormat` API – Wolfgang Kuehn Mar 24 '21 at 20:38
-
Yes, agreed, e.g., `new Intl.DateTimeFormat('en-US').format(date)` - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat – chrismarx May 25 '23 at 18:34
3 Answers
Try 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());

- 1,186
- 1
- 10
- 17

- 19,175
- 13
- 62
- 92
-
4edited above to account for the fact that javascript months are zero based...you must always add one to the month to get the exact month when displaying the data to a user. – Robert Petz Nov 21 '12 at 00:14
-
93that's not actually MM/dd/yyyy. That's M/d/yyyy. Does your date need leading zeros? – Ray Wadkins Sep 25 '13 at 21:25
-
6If using angular, you may want to consider: https://docs.angularjs.org/api/ng/filter/date – Soferio Jan 05 '15 at 02:43
-
1
-
Really consider using a library like Moment.js. It will format in desired result :) – Ankur Jan 28 '16 at 19:14
-
3Agree this answer is not correct. It does not produce mm/dd/yyyy. – Rob Breidecker Jul 07 '16 at 15:19
-
1why we have to add +1 in .getMonth() and not in other? i was suppose to add 90 days to current date, so date and year is getting calculated fine but not the month. I have to add +1 in getMonth, why so ? – Akshay Chawla Jun 19 '17 at 07:50
-
5Correct answer should be var result = ((date.getMonth().toString().length > 1) ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1))) + '/' + ((date.getDate().toString().length > 1) ? date.getDate() : ('0' + date.getDate())) + '/' + date.getFullYear() – Jyotirmaya Prusty Sep 13 '17 at 09:46
-
@AkshayChawla You have to add `1` to `date.getMonth()` because Jan is `0` – Kellen Stuart Dec 08 '17 at 16:04
-
Is there any JS method available to convert into specific date format – user2323308 Feb 19 '18 at 07:26
-
There is no need for a Date object, and this doesn't produce the required format and may well return different day, month and year values depending on the timezone it's run in. – RobG Jan 23 '19 at 09:38
-
-
1
-
This happened to me, I solved it using this: var date = new Date('2010-10-11T00:00:00+05:30'); var month = ("0"+(date.getMonth() + 1)).slice(-2); var day = ("0"+(date.getDate())).slice(-2); alert(month + '/' + day + '/' + date.getFullYear()); – micoru Jun 03 '19 at 15:05
-
3The correct answer will be : var result = ((date.getMonth() > 8) ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1))) + '/' + ((date.getDate() > 9) ? date.getDate() : ('0' + date.getDate())) + '/' + date.getFullYear(); This way we can reduce toString and length function calls. Also, the condition date.getMonth().toString().length > 1 will give wrong result as 010 for october month. – Pavan Yogi Sep 27 '19 at 08:06
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;
}

- 196,159
- 39
- 305
- 313

- 9,119
- 2
- 23
- 29
-
3This is returning '2013/15/06' for 'Sat Jun 01 2013 00:00:00 GMT-0400 (Eastern Summer Time)' – Code Monkey Jun 26 '13 at 13:21
-
4_@Ore4444_, you are converting month to string before you add 1, that's why _@Code Monkey_ says you are returning 15 instead of six. It should be `var month = (1 + date.getMonth()).toString();` – Moses Machua Jun 29 '13 at 19:43
-
3getDay returns the day of the week. Not the dates day for that you will need to use getDate() – gh9 Nov 22 '13 at 16:12
-
-
I like this answer better than the chosen answer. However, leading "0"s should also be added to `year` to handle years that are less than 4 digits long. – mikelt21 Oct 29 '14 at 16:27
-
-
-
Really consider using a library like Moment.js. It will format in desired result :) – Ankur Jan 28 '16 at 19:14
-
5@Ankur: instead copying your comment under each answer for the tip using Moment.js, you could give the code how it works with Moment.js.. – phil Feb 13 '17 at 08:40
-
-
I had trouble with this and found: https://stackoverflow.com/questions/4929382/javascript-getfullyear-is-not-a-function from this I was able to get my answer below working. – Glyn Aug 27 '18 at 02:20
-
1function getFormattedDate(date) { var start = new Date(date); var year = start.getFullYear(); var month = (1 + start.getMonth()).toString(); month = month.length > 1 ? month : '0' + month; var day = start.getDate().toString(); day = day.length > 1 ? day : '0' + day; return day + '/' + month + '/' + year; } – Glyn Aug 27 '18 at 02:21
-
you could also just use this to pad the numbers (see "2-digit" under month and day options). This will also ensure your date format matches the user's locale: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat – Paul Fabbroni Sep 09 '20 at 18:29
ISO compliant dateString
If the dateString is RFC282 and ISO8601 compliant:
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 compliant 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 compliant
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"

- 196,159
- 39
- 305
- 313
-
Really consider using a library like Moment.js. It will format in desired result :) – Ankur Jan 28 '16 at 19:14
-
1@RokoC.Buljan How to convert "Month dd, yyyy" into "MM/dd/yyyy" in javascript? – dilipkumar1007 May 12 '16 at 09:26
-
8Moment.js is *enormous* for simple stuff. Try date-format instead. Shaved 5 secs off my Webpack build time. – Eirik Birkeland Nov 07 '17 at 18:04
-
@EirikBirkeland nice suggestion. I abuse moment a lot for it's nice API and haven't even considered the amount of fluff. – rob2d Apr 21 '18 at 21:59
-
This doesn't take into account timezones which could lead your date, month, and year to be wrong. – applecrusher Mar 24 '20 at 15:32
-
anyone else notice we're in 2022 and something as simple as managing a date in JS is still such a cluster - library or not. – Dan Chase Jul 14 '22 at 19:32