0

I would like to extract date and time from each of these string. After extraction I want to convert them to iso format.

{
  date01: "2018-06-01T18:17:12.745+07:00",
  date02: "2018-06-01T18:17:12.745Z",
  date03: "2018-06-01T18:17:12",
  date04: "2018-06-01T18:17:12.745",
  date05: "2018-06-01+07:00",
  date06: "2018-06-01",
  date07: "www; 12-03-2018 года 11:00 часов",
  date08: "2018-05-17 года в 15:00 (по местному времени).",
  date09: "www; 12.03.2018 года 11:00 часов",
  date10: "2018.05.17 года в 15:00 (по местному времени).",
  date11: "www; 12-03-2018 года",
  date12: "2018-05-17 года",
  date13: "www.ru; 12.03.2018 года",
  date14: "2018.05.17 года",
  date15: "1 января 2017 года",
  date16: "11 августа 2018 года",
  date17: "02 дек. 2018 года",
  date18: '"02" ноя. 2018 года',
  date19: "«02» сен. 2018 года",
  date20: "27/03/2018 г. в 10:00 (по московскому времени)"
} 

I have tried using regex but it didn't work for some dates. I was able to convert from date01 to date04 without extraction. but date05 didn't work and it also has the time zone. date07,08,09,10 - I also extracted date and time, but time work during conversion to string.

ConvertFromStringToIsoFormat = (data) => {
   
    if(isNaN(data))
    {
        const date = new Date(data).toISOString()
        console.log(date)
    }else{
    //    const matchDate = /(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/
    //    const date = matchDate.exec(data)
    //    const matchTime = /(([0-1]?[0-9]|2[0-3]):([0-5][0-9]))/g
    //    const time = matchTime.exec(data)
    //    console.log(date[1], time[1])
    //    const isoTime = new Date(date[1], time[1]).toISOString()
    //    console.log(isoTime);
    }
       
}

ConvertFromStringToIsoFormat('2018-06-01T18:17:12.745+07:00')
// ConvertFromStringToIsoFormat('2018-06-01T18:17:12.745Z')
// ConvertFromStringToIsoFormat('2018-06-01T18:17:12')
ConvertFromStringToIsoFormat('2018-06-01T18:17:12.745')
// ConvertFromStringToIsoFormat('2018-06-01 +07:00')
ConvertFromStringToIsoFormat('2018-06-01')
ConvertFromStringToIsoFormat('27/03/2018 г. в 10:00 (по московскому времени)')
// ConvertFromStringToIsoFormat('ygyg gyg 2018-05-17 года в 15:00 (по местному времени).')
// ConvertFromStringToIsoFormat('11 августа 2018')

Is there a good practice solving this task? Please any help will be appreciated thanks in advance.

3
  • Can you post what you have done and for which one you need help? Commented Jul 30, 2021 at 8:03
  • Some are using local time zone (e.g. date08, unspecified, just "по местному времени", i.e. "by local time"), while others use UTC (e.g. date02), and still others are not mentioning timezone information (e.g. date01). This is not going to work if you have such mix of time zones, but don't know the exact time zone information. Commented Jul 30, 2021 at 8:03
  • My current timezone i.e. (по местному времени) is UTC + 7, and next one UTC + 3 Moscow time i.e. (по московскому времени). I have different timezone that's why I am confused this part as well. Commented Jul 30, 2021 at 8:12

1 Answer 1

2

My full code to convert it to iso string. Just add the months in the Russian language.

const dates = {
  date01: "2018-06-01T18:17:12.745+07:00",
  date02: "2018-06-01T18:17:12.745Z",
  date03: "2018-06-01T18:17:12",
  date04: "2018-06-01T18:17:12.745",
  date05: "2018-06-01+07:00",
  date06: "2018-06-01",
  date07: "www; 12-03-2018 года 11:00 часов",
  date08: "2018-05-17 года в 15:00 (по местному времени).",
  date09: "www; 12.03.2018 года 11:00 часов",
  date10: "2018.05.17 года в 15:00 (по местному времени).",
  date11: "www; 12-03-2018 года",
  date12: "2018-05-17 года",
  date13: "www.ru; 12.03.2018 года",
  date14: "2018.05.17 года",
  date15: "1 января 2017 года",
  date16: "11 августа 2018 года",
  date17: "02 дек. 2018 года",
  date18: '"02" ноя. 2018 года',
  date19: "«02» сен. 2018 года",
  date20: "27/03/2018 г. в 10:00 (по московскому времени)"
};
function convertMonth(month) {
  if (month.length > 3) {
    return (
      // add other months
      ["января", "", "", "", "", "", "", "августа", "", "", "", ""]
        .findIndex((m) => m === month) + 1
    );
  } else if (month.length === 3) {
    return (
      ["янв","" , "", "", "", "", "", "", "сен", "", "ноя", "дек"].findIndex(
        (m) => m === month
      ) + 1
    );
  }

  return Number(month);
}
function getDateAndTime(obj) {
  const cleanData = { ...obj };
  const returnedData = {};

  const regexDateSeparator = "[\\s./-]";
  const regexToClean = /[a-z]{3,}|[;'"»«]|\.(?=\s)/g;
  const regexDate = `\\d{1,4}${regexDateSeparator}(\\d{1,2}|[\wа-я]+)${regexDateSeparator}\\d{1,4}`;
  const regexTime = /\d{2}(:\d{2}){1,2}/;

  Object.keys(dates).forEach((date) => {
    cleanData[date] = cleanData[date].replaceAll(regexToClean, "");
    const [data] = cleanData[date].match(regexDate) || [""];
    const [D1, D2, D3] = data.split(new RegExp(regexDateSeparator));
    const [time] = cleanData[date].match(regexTime) || ["00:00:00"];
    const [h, m, s] = time.split(":");
    const newDate = new Date();
    newDate.setHours(h);
    newDate.setMinutes(m);
    if (s) {
      newDate.setSeconds(s);
    }

    // check date 2018-06-01 and 01-06-2018
    if (D1.length > 2) {
      newDate.setFullYear(D1);
      newDate.setDate(D3);
    } else {
      newDate.setFullYear(D3);
      newDate.setDate(D1);
    }

    newDate.setMonth(convertMonth(D2));

    returnedData[date] = newDate.toISOString();
  });

  return returnedData;
}

console.log(getDateAndTime(dates));

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried the iso conversion with the clean date and time but it didn't work for me. Were you able to do it ?
I see a problem once problem to fix. date01 have 7'th month. So just we need this to fix :)
Oh yeah, I have notice that all month has + 1
Idk where is the bug. Please fix my code if you find this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.