-1

The api I am working with requires time in the HH:MM:SS format however my output is simply showing a numeric value for the time(for example: 8:00am is output as 8). How can I convert this to HH:MM:SS format?

let targetStr = getTree().data.boards[0].groups[0].items[0].name;
let fields = ['DATE', 'TIME', 'DURATION', 'TYPE'];

console.log(extractData(targetStr, fields));

function extractData(str, fields) {
  return str.split(/\s*\|\s*/).reduce((res, entry) => {
    let dat = entry.split(/\s*:\s*/);
    return fields.indexOf(dat[0]) > -1 ? Object.assign(res, { [dat[0]]: dat[1] }) : res;
  }, {});
}

function getTree() {
  return {
    "data": {
      "boards": [{
        "owner": {
          "id": 555555555
        },
        "groups": [{
          "id": "new_group",
          "title": "Forecasts",
          "items": [{
            "id": "355670938",
            "name": "PIPE: Production & Client Management | STAGE: New Support Intake | NAME: TESTY | DATE: 10/27/2019 | TIME: 8:00 am | TIME ZONE: Central Time | DURATION: 60 minutes | TYPE: All Virtual | AUDIENCE VIEW:"
          }]
        }]
      }]
    },
    "account_id": 55555555
  };
}
klaurtar
  • 233
  • 3
  • 23
  • Did you consider using a lib? moment.js is quite useful when you come to work with date and time. Check it: https://momentjs.com/ – sjahan Oct 24 '19 at 15:34
  • Cause your data is flawed. : cannot occur in the fields, as it is part of the syntax. You could split with `": "`, but it would be better to take a syntax that supports this, e.g. JSON or XML. – Jonas Wilms Oct 24 '19 at 15:35
  • Are you able to control the output of the API? The API seems to be problematic, returning timezone unaware times in a problematic format (see above). IMO the best output format for API' is an ISO string: `2019-10-24T12:52:05+00:00`. This can be perfectly parsed by any decent library like `momentjs`. – Sven van de Scheur Oct 24 '19 at 15:54
  • No unfortunately. This is the worst API I have ever worked with – klaurtar Oct 24 '19 at 16:10

3 Answers3

2

let targetStr = getTree().data.boards[0].groups[0].items[0].name;
let fields = ['DATE', 'TIME', 'DURATION', 'TYPE'];

console.log(extractData(targetStr, fields));

function extractData(str, fields) {
    return str.split(/\s*\|\s*/).reduce((res, entry) => {
        var dat = entry.split(/\s*:\s*/);
        if (dat.length > 2) {
            dat = (dat + '').split(',');

            dat[2] = dat[2].split(' ');
            dat[1] = checkTime(dat[1], dat[2][1])
            dat[1] = dat[1].concat(':' + dat[2][0] + ':00')
        }
        return fields.indexOf(dat[0]) > -1 ? Object.assign(res, { [dat[0]]: dat[1] }) : res;
    }, {});
}
function checkTime(i, m) {
    if (i < 10) {
        i = "0" + i;
    }
    if (m == 'pm') {
        i = parseInt(i) + 12
    }
    return i + '';
}

function getTree() {
    return {
        "data": {
            "boards": [{
                "owner": {
                    "id": 555555555
                },
                "groups": [{
                    "id": "new_group",
                    "title": "Forecasts",
                    "items": [{
                        "id": "355670938",
                        "name": "PIPE: Production & Client Management | STAGE: New Support Intake | NAME: TESTY | DATE: 10/27/2019 | TIME: 8:00 am | TIME ZONE: Central Time | DURATION: 60 minutes | TYPE: All Virtual | AUDIENCE VIEW:"
                    }]
                }]
            }]
        },
        "account_id": 55555555
    };
}
NassimFS
  • 36
  • 3
0

Given the TIME-format is always like 'HH:MM (am|pm)' u can just reformat it like so:

let targetStr = getTree().data.boards[0].groups[0].items[0].name;
let fields = ['DATE', 'TIME', 'DURATION', 'TYPE'];
const data = extractData(targetStr, fields);
const [hours, minutes, amPm] = data.TIME.split(/[\s|:]/);
console.log({
  ...data,
  TIME: [
    (amPm === 'am') ? hours : (parseInt(hours) + 12),
    minutes,
    '00'
  ].join(':')
});


function extractData(str, fields) {
  return str.split(/\s*\|\s*/).reduce((res, entry) => {
    let [key, ...val] = entry.split(/\s*:\s*/);
    return fields.indexOf(key) > -1 ? Object.assign(res, { [key]: val.join(':') }) : res;
  }, {});
}
    
function getTree() {
  return {
    "data": {
      "boards": [{
        "owner": {
          "id": 555555555
        },
        "groups": [{
          "id": "new_group",
          "title": "Forecasts",
          "items": [{
            "id": "355670938",
            "name": "PIPE: Production & Client Management | STAGE: New Support Intake | NAME: TESTY | DATE: 10/27/2019 | TIME: 8:00 am | TIME ZONE: Central Time | DURATION: 60 minutes | TYPE: All Virtual | AUDIENCE VIEW:"
          }]
        }]
      }]
    },
    "account_id": 55555555
  };
}
Mischa
  • 1,591
  • 9
  • 14
  • it has to be in this format 08:00:00. So your solution would be perfect if there is a way to append a 0 in front of times before 10:00:00 – klaurtar Oct 24 '19 at 16:17
0

You can extract the h, m and am/pm parts, and increase h with 12 if it is pm. As the input has no seconds field, that is just appending a :00 at the end:

function hms(whatever){
  var parts=whatever.match(/TIME\:\s(\d+)\:(\d+)\s([^\s]+)/);
  var h=parseInt(parts[1]);
  var m=parts[2];
  if(parts[3]=="pm")h+=12;
  return ("0"+h).slice(-2)+":"+m+":00";
}

var test1="PIPE: Production & Client Management | STAGE: New Support Intake | NAME: TESTY | DATE: 10/27/2019 | TIME: 8:00 am | TIME ZONE: Central Time | DURATION: 60 minutes | TYPE: All Virtual | AUDIENCE VIEW";
var test2="PIPE: Production & Client Management | STAGE: New Support Intake | NAME: TESTY | DATE: 10/27/2019 | TIME: 8:00 pm | TIME ZONE: Central Time | DURATION: 60 minutes | TYPE: All Virtual | AUDIENCE VIEW";

console.log(hms(test1));
console.log(hms(test2));

(test2 is modified to pm).

tevemadar
  • 12,389
  • 3
  • 21
  • 49