Change date and format in Js

Thank you for those date format requirements.

Here’s some code that is capable of supporting all of those different types of formats.

// usage: const dmyhms = getDateTime("30-09-2022 13:14:15");
// result: {day: 30, month: 9, year: 2022, hour: 13, month: 14, second: 15}

function getDmyDate(datetimestring) {
    const dateRx = /(\d+)-(\d+)-(\d+)/;
    const match = datetimestring.match(dateRx);
    if (!match) {
        return {};
    }
    const [day, month, year] = match.slice(1);
    return {
        day: Number(day),
        month: Number(month),
        year: Number(year)
    };
}
function getYmdDate(datetimestring) {
    const dateRx = /(\d+)-(\d+)-(\d+)/;
    const match = datetimestring.match(dateRx);
    if (!match) {
        return {};
    }
    const [year, month, day] = match.slice(1);
    return {
        year: Number(year),
        month: Number(month),
        day: Number(day)
    };
}
function getTime(datetimestring) {
    const timeRx = /(\d+):(\d+):(\d+)/;
    const match = datetimestring.match(timeRx);
    if (!match) {
        return {hour: 0, minute: 0, second: 0};
    }
    const [hour, minute, second] = match.slice(1);
    return {
        hour: Number(hour),
        minute: Number(minute),
        second: Number(second)
    };
}
function getDateTime(datetimestring) {
  const datetime = {};
  if (datetimestring.substr(1, 1) === "-" ||
      datetimestring.substr(2, 1) === "-") {
      const date = getDmyDate(datetimestring);
      Object.assign(datetime, date);
  } else {
      Object.assign(datetime, getYmdDate(datetimestring));
  }
  Object.assign(datetime, getTime(datetimestring));
  return datetime;
}

The code can be found at https://jsfiddle.net/8hkqnyc2/