使用 Javascript 将小时转换为 HH:MM:SS

我正在使用的 api 需要 HH:MM:SS 格式的时间,但是我的输出只是显示时间的数值(例如:8:00am 输出为 8)。如何将其转换为 HH:MM:SS 格式?


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

  };

}


HUWWW
浏览 231回答 3
3回答

BIG阳

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) {&nbsp; &nbsp; return str.split(/\s*\|\s*/).reduce((res, entry) => {&nbsp; &nbsp; &nbsp; &nbsp; var dat = entry.split(/\s*:\s*/);&nbsp; &nbsp; &nbsp; &nbsp; if (dat.length > 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dat = (dat + '').split(',');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dat[2] = dat[2].split(' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dat[1] = checkTime(dat[1], dat[2][1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dat[1] = dat[1].concat(':' + dat[2][0] + ':00')&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return fields.indexOf(dat[0]) > -1 ? Object.assign(res, { [dat[0]]: dat[1] }) : res;&nbsp; &nbsp; }, {});}function checkTime(i, m) {&nbsp; &nbsp; if (i < 10) {&nbsp; &nbsp; &nbsp; &nbsp; i = "0" + i;&nbsp; &nbsp; }&nbsp; &nbsp; if (m == 'pm') {&nbsp; &nbsp; &nbsp; &nbsp; i = parseInt(i) + 12&nbsp; &nbsp; }&nbsp; &nbsp; return i + '';}function getTree() {&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; "data": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "boards": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "owner": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 555555555&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "groups": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "new_group",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title": "Forecasts",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "355670938",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "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:"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "account_id": 55555555&nbsp; &nbsp; };}

蝴蝶刀刀

鉴于 TIME 格式总是像 'HH:MM (am|pm)' 你可以像这样重新格式化它: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({&nbsp; ...data,&nbsp; TIME: [&nbsp; &nbsp; (amPm === 'am') ? hours : (parseInt(hours) + 12),&nbsp; &nbsp; minutes,&nbsp; &nbsp; '00'&nbsp; ].join(':')});function extractData(str, fields) {&nbsp; return str.split(/\s*\|\s*/).reduce((res, entry) => {&nbsp; &nbsp; let [key, ...val] = entry.split(/\s*:\s*/);&nbsp; &nbsp; return fields.indexOf(key) > -1 ? Object.assign(res, { [key]: val.join(':') }) : res;&nbsp; }, {});}&nbsp; &nbsp;&nbsp;function getTree() {&nbsp; return {&nbsp; &nbsp; "data": {&nbsp; &nbsp; &nbsp; "boards": [{&nbsp; &nbsp; &nbsp; &nbsp; "owner": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 555555555&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "groups": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "new_group",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title": "Forecasts",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "355670938",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "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:"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; },&nbsp; &nbsp; "account_id": 55555555&nbsp; };}

长风秋雁

您可以提取h,m和am/pm部分,h如果是,则增加12 pm。由于输入没有秒字段,所以只是:00在末尾附加一个:function hms(whatever){&nbsp; var parts=whatever.match(/TIME\:\s(\d+)\:(\d+)\s([^\s]+)/);&nbsp; var h=parseInt(parts[1]);&nbsp; var m=parts[2];&nbsp; if(parts[3]=="pm")h+=12;&nbsp; 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修改为pm)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript