Skip to content

Instantly share code, notes, and snippets.

@ashutoshpw
Last active September 7, 2022 12:56
Show Gist options
  • Save ashutoshpw/257ecd3815509ad0f8bac075c731bae5 to your computer and use it in GitHub Desktop.
Save ashutoshpw/257ecd3815509ad0f8bac075c731bae5 to your computer and use it in GitHub Desktop.
Google Sheets - Format time in format "2.50 - 3.00"
function processTimeString(time, isPostLunch){
time = time.trim().replace(".",":") + ":00"
const timeParts = time.split(":")
if(parseInt(timeParts[0]) < 12 && isPostLunch) time = `${(parseInt(timeParts[0])+12)}:${timeParts[1]}:${timeParts[2]}`
if(time.split(":")[0].length == 1) time = "0" + time;
return time
}
function processTime(time, isStart = true, isPostLunch = false){
const regex = /(\d{1,2}\.\d{2})[^\d]+(\d{1,2}\.\d{2})/gm;
const str = time;
let m, fromTime, toTime;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
if(groupIndex == 1) fromTime = ` ${match}`.slice(1)
if(groupIndex == 2) toTime = ` ${match}`.slice(1)
});
}
if(isStart)
return processTimeString(fromTime, isPostLunch)
return processTimeString(toTime, isPostLunch)
}
function W3DEV_STARTTIME(time, date, isPostLunch = false) {
const startTime = processTime(time, true, isPostLunch)
return `${date} ${startTime}`
}
function W3DEV_ENDTIME(time, date, isPostLunch = false) {
const endTime = processTime(time, false, isPostLunch)
return `${date} ${endTime}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment