简体   繁体   中英

Converting hour to HH:MM:SS with Javascript

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
  };
}

 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 }; }

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 }; }

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 ).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM