142

How to convert a date in format 23/10/2015 into a JavaScript Date format:

Fri Oct 23 2015 15:24:53 GMT+0530 (India Standard Time)
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Raja Manickam
  • 1,743
  • 4
  • 20
  • 28

10 Answers10

286

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.

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

var dateObject = new Date(dateString);

document.body.innerHTML = dateObject.toString();

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):

var 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();

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:

var 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();
<script src="https://momentjs.com/downloads/moment.min.js"></script>

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.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • 2
    you should pass the correct type (number) to the constructor. var dateObject: Date = new Date(+dateParts[2], +dateParts[1] - 1, +dateParts[0]); – Daniele Urania Jul 23 '17 at 08:38
  • This one is giving Fri Oct 23 2015 00:00:00 GMT+0530 (IST), And if we need "23 Oct 2015" only not complete string – Pankaj Bhardwaj Nov 16 '17 at 07:17
  • 47
    There's only *one* country in the whole world using MM/DD/YYYY and what do they set as the default JavaScript format? So annoying. – Hankrecords Jan 23 '20 at 13:37
  • 1
    you can change the date format from dd/mm/yyyy to mm/dd/yyyy and vice versa, using regex, with something like this: const originalDate = '23/07/2020'; const newDate = originalDate.replace(/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/g,'$2/$1/$3'); // newDate = '07/23/2020'; – Tomás Hugo Almeida Jul 23 '20 at 09:20
  • You can no longer depend on JavaScript parsing MM/DD/YYYY natively, due to changes in the underlying ECMAScript spec. The formats a JavaScript engine will parse are implementation-dependent, with the only ones in the ECMAScript standard being the IETF standard and a form of ISO 8601 (see [What string date format will javascript's parse recognize?](https://stackoverflow.com/q/5418631/215552)) – Heretic Monkey Jul 07 '22 at 13:45
  • For me, This is not viable as for production I had to parse 1000+ dates for some orders with further processing. Still looking for other solutions but if your data set is small you can use this snippet. `var dateString = "23/10/2015"; const correctDateString = new Date(dateString.split('/').reverse().join('/')).toString() \\ 'Fri Oct 23 2015 00:00:00 GMT+0530 (India Standard Time)'` – ovyas24 Oct 06 '22 at 03:19
  • Why no1 recommends doing this like new Date().toLocaleDateString('en-UK') ? – Mihai Dec 05 '22 at 20:36
12

Here's one I prepared earlier...

  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;     
  }
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
4
var dateString = "23/10/2015"; // Oct 23
var newData = dateString.replace(/(\d+[/])(\d+[/])/, '$2$1');
var data = new Date(newData);

document.body.innerHTML = date.toString();ere
drgol
  • 69
  • 2
3

While most responses were tied to splitting strings or using native date methods, the two closely-related ones using RegEx (i.e., answer by [drgol] and comment by [Tomás Hugo Almeida]) are both instructive about the use of capturing groups. Their succinctness also helps illustrate the value of capturing and distinguishing that from matching - two related concepts that can confuse new RegEx users. This code block consolidates their 2 answers but see originals above: const origDate = '23/07/2020'; const newDate = origDate.replace(/(\d+[/])(\d+[/])/, '$2$1'); // newDate = '07/23/2020';

3

Parsing a string to create another string that is then parsed by the built–in parser is not an efficient strategy, particularly when neither string is in a format supported by ECMA-262.

A more efficient strategy is to parse the string once and give the parts directly to the constructor, avoiding the second parse, e.g.

const parseDMY = s => {
  let [d, m, y] = s.split(/\D/);
  return new Date(y, m-1, d);
};

console.log(parseDMY('23/10/2015').toString());

Date.parse only supports the formats produced by:

  1. Date.protoype.toString
  2. Date.protoype.toISOString
  3. Date.protoype.toUTCString

Parsing of any other format (including m/d/y) is implementation dependent.

RobG
  • 142,382
  • 31
  • 172
  • 209
2

I found the default JS date formatting didn't work.

So I used toLocaleString with options

const event = new Date();
const options = { dateStyle: 'short' };
const date = event.toLocaleString('en', options);

to get: DD/MM/YYYY format

See docs for more formatting options: https://www.w3schools.com/jsref/jsref_tolocalestring.asp

0

Here is a way to transform a date string with a time of day to a date object. For example to convert "20/10/2020 18:11:25" ("DD/MM/YYYY HH:MI:SS" format) to a date object

    function newUYDate(pDate) {
      let dd = pDate.split("/")[0].padStart(2, "0");
      let mm = pDate.split("/")[1].padStart(2, "0");
      let yyyy = pDate.split("/")[2].split(" ")[0];
      let hh = pDate.split("/")[2].split(" ")[1].split(":")[0].padStart(2, "0");
      let mi = pDate.split("/")[2].split(" ")[1].split(":")[1].padStart(2, "0");
      let secs = pDate.split("/")[2].split(" ")[1].split(":")[2].padStart(2, "0");
    
      mm = (parseInt(mm) - 1).toString(); // January is 0
    
      return new Date(yyyy, mm, dd, hh, mi, secs);
    }
brookemitchell
  • 819
  • 9
  • 16
Franco Fontana
  • 115
  • 1
  • 6
-2

you can use this short function

// dateString: "15/06/2021"
  

const stringToDate = (dateString) => {
  const [day, month, year] = dateString.split('/');
  return new Date([month, day, year].join('/'));
};

document.body.innerHTML = stringToDate("15/06/2021").toString();
A. Hamdy
  • 39
  • 3
  • This doesn't answer the question, what this does is convert a dd/mm/yyyy into a mm/dd/yyyy string, instead of just returning the Date object. Also, this is the same as in the accepted answer. – Nate Levin Jun 15 '21 at 17:34
  • 1
    This is simpler es6 way to do it, I think that you didn't see the return new Date which acaxtly the answer – A. Hamdy Jun 15 '21 at 22:39
  • Because `new Date(year, month-1, day)` is too easy? :-) – RobG Jul 07 '22 at 13:28
-4
var date = new Date("enter your  date");//2018-01-17 14:58:29.013

Just one line is enough no need to do any kind of split, join, etc.:

$scope.ssdate=date.toLocaleDateString();//  mm/dd/yyyy format
clemens
  • 16,716
  • 11
  • 50
  • 65
-4
<!DOCTYPE html>

<script>

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

d = dateString.split("/");

x = d[1] + "/" + d[0] + "/" + d[2];    //"10/23/2015"
y = d[2] + "/" + d[1] + "/" + d[0];    //"2015/10/23"

alert(
new Date(x) + "\n\n" +
new Date(y) + "\n\n" +
new Date(dateString) + "\n" +
"");

</script>
User
  • 71
  • 1
  • 7