0

I have a list of dates that are of the following form '12:30 22/4/2020'. I've tried converting them using regular expressions and answers that I found online but I couldn't get any of them to work. How can I transform this string into a DateTime object?

  • It can be useful: https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript – Aurélien May 08 '20 at 17:47
  • @Aurélien Thanks for the link, i've tried many of the answers that i found and none worked. I was hoping for a concrete answer –  May 08 '20 at 17:47
  • What **exactly** have you tried that didn't work? There are *lots* of questions and answers on the topic here and elsewhere. – Pointy May 08 '20 at 17:48
  • I've tried the standard methods that JS offers, like `new Date()`, `Date.parse()` and a few more. None worked cause of the form of the string. I've also tried hacking regular expressions but I've never worked with those before so i didn't get anywhere –  May 08 '20 at 17:50
  • The linked question has plenty of information, including suggestions to look into Moment.js, a library that provides customizable date parsing facilities. – Pointy May 08 '20 at 17:52

3 Answers3

1

You can use a library like Moment.js to parse your string:

const str1 = '12:30 22/4/2020';
const str2 = '12:30 1/5/2020';

const date1 = moment(str1, 'HH:mm DD/MM/YYYY');
const date2 = moment(str2, 'HH:mm DD/MM/YYYY');

console.log(date1.format());
console.log(date2.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment.min.js"></script>
Fraction
  • 11,668
  • 5
  • 28
  • 48
  • the top conversion in your example returns 1st of April instead of 22nd? –  May 08 '20 at 18:11
0

Or you can write your own function, specially for that case, without any libraries:

function convert(str) {
  let temp = str.split(" ");
  let time = temp[0].split(":");
  let date = temp[1].split("/");
  return new Date(date[2], date[1], date[0], time[0], time[1]);
}

console.log(convert("16:20 31/2/2022").toString())
// "Thu Mar 31 2022 16:20:00 GMT+0300"
Xth
  • 710
  • 1
  • 5
  • 11
0

I recommend you to use some library like moment.js or date-fns for those things but if you are trying to avoid using 3rd party library below you can see an example doing of doing that without any regex.

// with date-fns 2.13.0 (recommended)
const {parse} = require("date-fns");

const fullDateStr = '12:30 22/4/2020';
const parsedFnsDate = parse(fullDateStr, 'H:m dd/M/yyyy', new Date());

// if format of the hours and minutes is 1..10
console.log('with parse lib', parsedFnsDate);

// for more details see the docs of parse https://date-fns.org/v2.13.0/docs/parse

// with pure js without regex

const [timeStr, dateStr] = fullDateStr.split(' ');
const [hourStr, minuteStr] = timeStr.split(':');
const [dayStr, monthStr, yearStr] = dateStr.split('/');
const parsedNoRegexDate = new Date(
  `${yearStr}-${monthStr}-${dayStr} ${hourStr}:${minuteStr}`
);

console.log(parsedNoRegexDate);
Fetz
  • 1,196
  • 8
  • 11