By default Date.parse consider in this format that month precede the day to be in your case MM/DD/YYYY not as you want DD/MM/YYYY.

I prefer/suggest using 3rd party date parser library as Moment.js

It can take your date-string and the format to be like this:

moment("10/11/2016", "DD-MM-YYYY");
Answer from Basim Hennawi on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › parse
Date.parse() - JavaScript | MDN
See the linked reference for caveats ... This function is useful for setting date values based on string values, for example in conjunction with the setTime() method....
🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format: const d = new Date("2015-03-25"); Try it Yourself » · The computed date will be relative to your time zone.
🌐
Forum ElectroNeek
forum.electroneek.com › rpa automation › studio pro
Parse Date to Specific Cultures (JavaScript) - Studio Pro - Forum ElectroNeek
December 19, 2023 - Hi, I’m trying to parse a Date to a specific format (language/culture) without having to do lots of manual manipulations of the data, so I wanted to use the library “momentjs”. However, I tried many forms and I couldn’t import the library to use it. You can’t import libraries for JavaScript ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
See the Formats of toString method return values section for examples. ... When called as a constructor, returns a new Date object. When called as a function, returns a string representation of the current date and time. ... Returns the numeric value corresponding to the current time—the ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-string-to-date-date-parsing-in-js
JavaScript String to Date – Date Parsing in JS
June 29, 2022 - Date.parse() tells us the number of milliseconds that have elapsed since January 1, 1970. This is helpful when comparing multiple dates.
🌐
Index.dev
index.dev › blog › convert-string-to-date-javascript
6 Easy Ways To Convert String to Date in JavaScript
In the absence of a specified timezone, it defaults to the local timezone of the executing environment. An alternative approach to turn a string into a date is by · using the Date.parse() function.
🌐
Chris Pietschmann
pietschsoft.com › post › 2023 › 09 › 28 › javascript-parse-string-to-a-date
JavaScript: Parse a String to a Date | Chris Pietschmann
September 28, 2023 - The most reliable way to parse a date string in JavaScript is to use the ISO date format. ISO format can be in the form of YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS. However, there’s a caveat: JavaScript may interpret the input string as either UTC ...
Find elsewhere
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.

🌐
Day.js
day.js.org › docs › en › parse › string-format
String + Format · Day.js
Pass the locale key as the third parameter to parse locale-aware date time string. require('dayjs/locale/es') dayjs('2018 Enero 15', 'YYYY MMMM DD', 'es') You may specify a boolean for the last argument to use strict parsing. Strict parsing requires that the format and input match exactly, including delimiters.
🌐
W3Schools
w3schools.com › jsref › jsref_parse.asp
JavaScript Date parse() Method
// Calculate milliseconds in a year const minute = 1000 * 60; const hour = minute * 60; const day = hour * 24; const year = day * 365; // Compute years const d = Date.parse("March 21, 2012"); let years = Math.round(d / year); Try it Yourself » · parse() is an ECMAScript1 (JavaScript 1997) feature.
🌐
Byby
byby.dev › js-format-date
How to parse and format a date in JavaScript
Once a date is parsed in JavaScript and converted to a Date object, it can then be formatted into a string with a specific date format.
🌐
GitHub
gist.github.com › 6039800
#JS: create date objects with custom date formats · GitHub
#JS: create date objects with custom date formats. GitHub Gist: instantly share code, notes, and snippets.
🌐
Zipy
zipy.ai › blog › parsing-a-string-to-a-date-in-javascript
parsing a string to a date in javascript
April 12, 2024 - Parsing is heavily dependent on the format of the date string, which might lead to inconsistencies across different locales and browsers. Another common approach is directly passing the date string to the Date constructor. The JavaScript engine tries to parse the string based on recognized ISO 8601 formats and other commonly used variations:
Top answer
1 of 16
1087

The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor.

Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.

But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best practice should always be to store dates as UTC and make computations as UTC.

To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z').

To display a date in UTC, use .toUTCString(),
to display a date in user's local time, use .toString().

More info on MDN | Date and this answer.

For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00'). Note that the number of the month must be 1 less.


Alternate method - use an appropriate library:

You can also take advantage of the library Moment.js that allows parsing date with the specified time zone.

2 of 16
452

Unfortunately I found out that

var mydate = new Date('2014-04-03');
console.log(mydate.toDateString());

returns "Wed Apr 02 2014". I know it sounds crazy, but it happens for some users.

The bulletproof solution is the following:

var parts ='2014-04-03'.split('-');
// Please pay attention to the month (parts[1]); JavaScript counts months from 0:
// January - 0, February - 1, etc.
var mydate = new Date(parts[0], parts[1] - 1, parts[2]); 
console.log(mydate.toDateString());

🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Date and time
The method Date.parse(str) can read a date from a string. The string format should be: YYYY-MM-DDTHH:mm:ss.sssZ, where:
🌐
Tabnine
tabnine.com › home › how to use date.parse in javascript
How to Use Date.parse in JavaScript - Tabnine
July 25, 2024 - The ISO 8601 date format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ. The characters represent the following data: YYYY – the current year, in four digits (e.g. 2020) MM – the current month, with a leading 0 (e.g.
🌐
npm
npmjs.com › package › date-format-parse
date-format-parse - npm
import { format } from 'date-format-parse'; format(new Date(), 'YYYY-MM-DD HH:mm:ss.SSS'); // with locale, see locale config below const obj = { ...
      » npm install date-format-parse
    
Published   Aug 08, 2021
Version   0.2.7
Author   xiemengxiong