160

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.

Brett DeWoody
  • 50,328
  • 25
  • 121
  • 168
Pradeep
  • 3,962
  • 9
  • 36
  • 49
  • 10
    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 Nov 27 '16 at 14:09
  • 1
    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 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
  • The correct solution nowadays is to use the `Intl.DateTimeFormat` API – Wolfgang Kuehn Mar 24 '21 at 20:38

3 Answers3

279

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());
Ajeet Eppakayala
  • 835
  • 1
  • 4
  • 14
yogi
  • 17,657
  • 12
  • 53
  • 89
  • 4
    edited 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
  • 89
    that'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
  • 6
    If using angular, you may want to consider: https://docs.angularjs.org/api/ng/filter/date – Soferio Jan 05 '15 at 02:43
  • 1
    This answer is wrong. See my answer below. – Ore4444 Jan 13 '16 at 12:39
  • Really consider using a library like Moment.js. It will format in desired result :) – Ankur Jan 28 '16 at 19:14
  • 3
    Agree this answer is not correct. It does not produce mm/dd/yyyy. – Rob Breidecker Jul 07 '16 at 15:19
  • 1
    why 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
  • 5
    Correct 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` – Kolob Canyon 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
  • See my answer below, which is also updated to ES2019 – Ore4444 Feb 03 '19 at 09:26
  • 1
    @JyotirmayaPrusty no it's not. `10` will become `010` – Tenzolinho Apr 18 '19 at 12:38
  • 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
  • 2
    The 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
170

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.

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;
}
Lucas
  • 129
  • 10
Ore4444
  • 8,291
  • 2
  • 19
  • 29
  • 3
    This 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
  • 3
    getDay 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
  • Thanks @gh9 and M. Machua. I integrated your comments into the code – Ore4444 Nov 24 '13 at 08:51
  • 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
  • This is yyyy/MM/dd. Closer than accepted answer though. – hamboy Jan 12 '16 at 18:45
  • Thanks @hamboy. Corrected. – Ore4444 Jan 13 '16 at 12:37
  • Really consider using a library like Moment.js. It will format in desired result :) – Ankur Jan 28 '16 at 19:14
  • 4
    @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
  • thank you @Ore4444 it is working perfectly – Ilaria Jun 05 '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
  • 1
    function 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
44

ISO compliant dateString

If your dateString is RFC282 and ISO8601 compliant:
pass your string into the Date Constructor:

const dateString = "2020-10-30T12:52:27+05:30"; // ISO8601 compliant dateString
const D = new Date(dateString);                 // {object Date}

from here you can extract the desired values by using Date Getters:

D.getMonth() + 1  // 10 (PS: +1 since Month is 0-based)
D.getDate()       // 30
D.getFullYear()   // 2020

Non-standard date string

If you use a non standard date string:
destructure the string into known parts, and than pass the variables to the Date Constructor:

new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])

const dateString = "30/10/2020 12:52:27";
const [d, M, y, h, m, s] = dateString.match(/\d+/g);

// PS: M-1 since Month is 0-based
const D = new Date(y, M-1, d, h, m, s);  // {object Date}


D.getMonth() + 1  // 10 (PS: +1 since Month is 0-based)
D.getDate()       // 30
D.getFullYear()   // 2020

Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278