Reputation: 65
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.
Upvotes: 0
Views: 423
Reputation: 1233
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));
Upvotes: 2