user16860065
user16860065

Reputation:

Add string content to array of dates using map

How can I add strings that are separated using colon present in two input fields with the format hh:mm:ss to a range of dates selected that is stored something like this, ["11.10.2022 23:43:24","28.10.2022 23:43:24"] . I am using dayjs for date related actions. Help will be appreciated

Code that I tried

const from = "12:49:55";
const to = "10:45:55";
const range = ["11.10.2022 23:43:24","28.10.2022 23:43:24"];
const timeAdded = range?.map(
        (value: Dayjs, index: number) => {
          if (index === 0) {
            return dayjs(from, "hour").add(from, "minute").add(from, "second");
          }
          if (index === 1) {
            return dayjs(to, "hour").add(to, "minute").add(to, "second");
          }
        }
 );

How can I add both the values in one go. Output should look like

["11.10.2022 12:49:55","28.10.2022 10:45:55"]

Upvotes: 0

Views: 39

Answers (1)

Carsten Massmann
Carsten Massmann

Reputation: 28196

You can do it without days.js:

const from="12:49:55", to="10:45:55";
const times=[from,to];
const range = ["11.10.2022 23:43:24","28.10.2022 23:43:24"];
const timeAdded = range.map((r,i)=>r.replace(/ .*/," "+times[i]));

console.log(timeAdded);

Upvotes: 1

Related Questions