I have a google sheet column which has the duration column in the format as like the below. 4w 1d 19h 56m 16s
. How can I convert this in total minutes/seconds?
PS: Every value in that list will be optional. Meaning, Sometime Weeks will be missing and days will be missing etc.
If the column values are simple strings and you're using Apps Script, you may be able to do something like this:
function transformDurationString(dString) {
const multipliers = {
s: 1,
m: 60,
h: 60 * 60,
d: 60 * 60 * 24,
w: 60 * 60 * 24 * 7,
};
let totalSeconds = 0;
let durations = dString.split(" ");
for (const duration of durations) {
const durationValue = Number(duration.slice(0, -1));
const durationType = duration.slice(-1);
totalSeconds += durationValue * multipliers[durationType];
}
const totalMinutes = Math.floor(totalSeconds / 60);
const remainderSeconds = totalSeconds % 60;
return `${totalMinutes}m ${totalSeconds}s`;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.