You might want to use helper library like http://momentjs.com/ which wraps the native javascript date object for easier manipulations

Then you can do things like:

var day = moment("12-25-1995", "MM-DD-YYYY");

or

var day = moment("25/12/1995", "DD/MM/YYYY");

then operate on the date

day.add('days', 7)

and to get the native javascript date

day.toDate();
Answer from user1372449 on Stack Overflow
Top answer
1 of 5
89

You might want to use helper library like http://momentjs.com/ which wraps the native javascript date object for easier manipulations

Then you can do things like:

var day = moment("12-25-1995", "MM-DD-YYYY");

or

var day = moment("25/12/1995", "DD/MM/YYYY");

then operate on the date

day.add('days', 7)

and to get the native javascript date

day.toDate();
2 of 5
81

Update

Below you've said:

Sorry, i can't predict date format before, it should be like dd-mm-yyyy or dd/mm/yyyy or dd-mmm-yyyy format finally i wanted to convert all this format to dd-MMM-yyyy format.

That completely changes the question. It'll be much more complex if you can't control the format. There is nothing built into JavaScript that will let you specify a date format. Officially, the only date format supported by JavaScript is a simplified version of ISO-8601: yyyy-mm-dd, although in practice almost all browsers also support yyyy/mm/dd as well. But other than that, you have to write the code yourself or (and this makes much more sense) use a good library. I'd probably use a library like moment.js or DateJS (although DateJS hasn't been maintained in years).


Original answer:

If the format is always dd/mm/yyyy, then this is trivial:

var parts = str.split("/");
var dt = new Date(parseInt(parts[2], 10),
                  parseInt(parts[1], 10) - 1,
                  parseInt(parts[0], 10));

split splits a string on the given delimiter. Then we use parseInt to convert the strings into numbers, and we use the new Date constructor to build a Date from those parts: The third part will be the year, the second part the month, and the first part the day. Date uses zero-based month numbers, and so we have to subtract one from the month number.

🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
Independent of input format, JavaScript will (by default) output dates in full text string format: ISO 8601 is the international standard for the representation of dates and times. The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format:
🌐
Day.js
day.js.org › docs › en › display › format
Format · Day.js
dayjs().format() // current date in ISO8601, without fraction seconds e.g. '2020-04-02T08:02:17-05:00' dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]') // 'YYYYescape 2019-01-25T00:00:00-02:00Z' dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
🌐
Byby
byby.dev › js-format-date
How to parse and format a date in JavaScript
JavaScript provides several methods ... must be in a specific format that can be recognized by the parsing method. Common formats include ISO 8601, RFC 2822, and short date formats such as mm/dd/yyyy ......
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › parse
Date.parse() - JavaScript | MDN
// Standard date-time string format const unixTimeZero = Date.parse("1970-01-01T00:00:00Z"); // Non-standard format resembling toUTCString() const javaScriptRelease = Date.parse("04 Dec 1995 00:12:00 GMT"); console.log(unixTimeZero); // Expected output: 0 console.log(javaScriptRelease); // Expected output: 818035920000
🌐
Isotropic
isotropic.co › how-to-format-a-date-as-dd-mm-yyyy-in-javascript
How To Format a Date as DD/MM/YYYY in JavaScript - Isotropic
The most straightforward way to create a date string in DD/MM/YYYY format involves using a few native get() methods and some simple logic. The first step is to get the date’s month, date, and year. Be careful here — JavaScript months are 0 based so you’ll need to increment the month by one.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript-and-append-it-to-an-input
How to get current formatted date dd/mm/yyyy in Javascript and append it to an input? - GeeksforGeeks
July 11, 2025 - To display dd/mm/yyyy, set the locale to 'en-GB'. Use the valueAsDate property to assign the current date as a Date object to an input field. ... Example: In this example we displays the current date using JavaScript's toLocaleDateString() method ...
🌐
Day.js
day.js.org › docs › en › parse › string-format
String + Format · Day.js
Strict parsing requires that the format and input match exactly, including delimiters. dayjs('1970-00-00', 'YYYY-MM-DD').isValid() // true dayjs('1970-00-00', 'YYYY-MM-DD', true).isValid() // false dayjs('1970-00-00', 'YYYY-MM-DD', 'es', true).isValid() // false
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript
How to Get Current Formatted Date dd/mm/yyyy in JavaScript? | GeeksforGeeks
December 4, 2024 - ... The current date in "dd/mm/yyyy" format represents the day, month, and year according to the Gregorian calendar, to obtain the current date in "dd/mm/yyyy" format using JavaScript, utilize the Date object methods to extract day, month, and ...
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-format-date-dd-mm-yyyy
How to Format a Date as DD/MM/YYYY in JavaScript | bobbyhadz
You can also use the date-fns module to format a date as DD/MM/YYYY. First, make sure you have the module installed. Open your terminal in your project's root directory (where your package.json file is) and run the following command to install ...
🌐
Enterprise DNA
blog.enterprisedna.co › how-to-convert-string-to-date-in-javascript-in-dd-mmm-yyyy-format
How to Convert String to Date in Javascript in dd-mmm-yyyy Format – Master Data Skills + AI
Stuck on converting a string to a date in Javascript, let’s sort that out. You can convert strings to dates in the “DD-MMM-YYYY” format using the Date() constructor or the Date.parse() method.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › date-tostring-format-yyyy-mm-dd
Format a JavaScript Date to YYYY MM DD - Mastering JS
To format a date to YYYYMMDD in JavaScript, you can use the toLocaleDateString() function in combination with the split(), reverse(), and join() functions. The trick is that, in the UK, dates are formatted in DD/MM/YYYY format, with two digit ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-format-a-date-with-javascript-date-formatting-in-js
How to Format a Date with JavaScript – Date Formatting in JS
November 7, 2024 - Specific Date Format: To display a date in a specific format, such as DD/MM/YYYY, you can use Intl.DateTimeFormat with the appropriate options.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
The format is as follows: ... YYYY is the year, with four digits (0000 to 9999), or as an expanded year of + or - followed by six digits. The sign is required for expanded years. -000000 is explicitly disallowed as a valid year. MM is the month, with two digits (01 to 12).
Top answer
1 of 10
308

MM/DD/YYYY format

If you have the MM/DD/YYYY format which is default for JavaScript, you can simply pass your string to Date(string) constructor. It will parse it for you.

Copyvar dateString = "10/23/2015"; // Oct 23

var dateObject = new Date(dateString);

document.body.innerHTML = dateObject.toString();
Run code snippetEdit code snippet Hide Results Copy to answer Expand

DD/MM/YYYY format - manually

If you work with this format, then you can split the date in order to get day, month and year separately and then use it in another constructor - Date(year, month, day):

Copyvar dateString = "23/10/2015"; // Oct 23

var dateParts = dateString.split("/");

// month is 0-based, that's why we need dataParts[1] - 1
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]); 

document.body.innerHTML = dateObject.toString();
Run code snippetEdit code snippet Hide Results Copy to answer Expand

For more information, you can read article about Date at Mozilla Developer Network.

DD/MM/YYYY - using moment.js library

Alternatively, you can use moment.js library, which is probably the most popular library to parse and operate with date and time in JavaScript:

Copyvar dateString = "23/10/2015"; // Oct 23

var dateMomentObject = moment(dateString, "DD/MM/YYYY"); // 1st argument - string, 2nd argument - format
var dateObject = dateMomentObject.toDate(); // convert moment.js object to Date object

document.body.innerHTML = dateObject.toString();
Copy<script src="https://momentjs.com/downloads/moment.min.js"></script>
Run code snippetEdit code snippet Hide Results Copy to answer Expand

In all three examples dateObject variable contains an object of type Date, which represents a moment in time and can be further converted to any string format.

2 of 10
14

Here's one I prepared earlier...

Copy  convertToDate(dateString) {
      //  Convert a "dd/MM/yyyy" string into a Date object
      let d = dateString.split("/");
      let dat = new Date(d[2] + '/' + d[1] + '/' + d[0]);
      return dat;     
  }
🌐
GitHub
github.com › date-fns › date-fns › issues › 720
Parse does not correctly handle DD/MM/YYYY format · Issue #720 · date-fns/date-fns
January 11, 2018 - > dateFns.parse('02/11/2014', 'MM/DD/YYYY', new Date()) Tue Feb 11 2014 00:00:00 GMT+0000 (GMT) > dateFns.parse('02/11/2014', 'DD/MM/YYYY', new Date()) Tue Feb 11 2014 00:00:00 GMT+0000 (GMT) Second one should be Sun Nov 02 2014 00:00:00 GMT+0000 (GMT). 👍React with 👍2tqwewe and PhoenixPan ·
Author   tvararu
🌐
Moment.js
momentjs.com › docs
Moment.js | Docs
Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing. Prefer formats earlier in the array than later. moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]); // uses the last format moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"]); // uses the first format