Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert dd/mm/yyyy string into JavaScript Date object?

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)
like image 668
Raja Manickam Avatar asked Oct 23 '15 10:10

Raja Manickam


4 Answers

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.

like image 95
Yeldar Kurmangaliyev Avatar answered Nov 05 '22 10:11

Yeldar Kurmangaliyev


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;     
  }
like image 40
Mike Gledhill Avatar answered Nov 05 '22 11:11

Mike Gledhill


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
like image 4
drgol Avatar answered Nov 05 '22 11:11

drgol


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';

like image 3
Glenn Gutmacher Avatar answered Nov 05 '22 11:11

Glenn Gutmacher