Convert timestring to number Google Spreadsheet

function toMS(input) {
  const sRegex = /\d+s/g;
  const msRegex = /\d+ms/g;
  const usRegex = /\d+us/g;
  
  Logger.log("Running toMS with parameter: " + input);

  var sToMs = 0;
  var msToMs = 0;
  var usToMs = 0;
  
  while((seg = sRegex.exec(input)) !== null) {
    sToMs += parseInt(seg) * 1000;
  } 
  while((seg = msRegex.exec(input)) !== null) {
    msToMs += parseInt(seg);
  }
  while((seg = usRegex.exec(input)) !== null) {
    usToMs += parseInt(seg) / 1000;
  }
  
  usToMs += msToMs + sToMs;
  
  return usToMs;
}

function toUS(input) {
  return toMS(input) * 1000;
}

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Spelling error report

The following text will be sent to our editors: