Javascript 从军事时间转换为实际时间,如用户';经验

Javascript 从军事时间转换为实际时间,如用户';经验,javascript,Javascript,我正在制定课程表。我试图测试时间过滤器是否有效。下面是API。我需要做的是将“h”从api中删除一小时。让我们深入到显示开始时间和结束时间的会议中。我决定将它们转换为实际时间,不包括“h” var filteredObject = [{ class_number: "17002", subject: "COMP", catalog_number: "110L", section_number: "21", title: "Introduction to A

我正在制定课程表。我试图测试时间过滤器是否有效。下面是API。我需要做的是将“h”从api中删除一小时。让我们深入到显示开始时间和结束时间的会议中。我决定将它们转换为实际时间,不包括“h”

var filteredObject = [{
    class_number: "17002",
    subject: "COMP",
    catalog_number: "110L",
    section_number: "21",
    title: "Introduction to Algorithms and Programming Lab",
    course_id: 15268,
    description: "Prerequisites: Grade of C or better in MATH 102 103  104 105 150A or 255A or a passing score on the Math Placement Test (MPT) that satisfies prerequisites for MATH 150A or 255A . Corequisites: COMP 110. Introduction to algorithms their representation design structuring analysis and optimization. Implementation of algorithms as structured programs in a high level language. Lab: three hours per week. (Available for General Education Lifelong Learning if required by students major.)\n",
    units: "1",
    term: "Fall-2019",
    class_type: "LAB",
    enrollment_cap: 20,
    enrollment_count: 20,
    waitlist_cap: 0,
    waitlist_count: 0,
    meetings: [{
        meeting_number: 1,
        location: "JD1104",
        "start_time": "1730h",
        "end_time": "1845h",
        days: "MW"
    }],
    instructors: [{
        instructor: "bahram.zartoshty@csun.edu"
    }]
}, ];
因此,我为convert函数添加了代码段。下面是隐蔽功能的代码:

//var time = "16:30:00"; // input as example
//time = time.split(':'); // convert to array

filteredObject[0].meetings[0].start_time.split(':') //convert to array
// fetch
var hours = Number(filteredObject[0]);
var minutes = Number(filteredObject[1]);
var seconds = Number(filteredObject[2]);

// calculate
var timeValue;
if (hours > 0 && hours <= 12) {
    timeValue = "" + hours;
} else if (hours > 12) {
    timeValue = "" + (hours - 12);
} else if (hours == 0) {
    timeValue = "12";
}
timeValue += minutes < 10 ? ":0" + minutes : ":" + minutes; // get minutes
timeValue += seconds < 10 ? ":0" + seconds : ":" + seconds; // get seconds
timeValue += hours >= 12 ? " P.M." : " A.M."; // get AM/PM
//show
console.log(timeValue);
//var time=“16:30:00”;//以输入为例
//时间=时间。拆分(“:”);//转换为数组
filteredObject[0]。会议[0]。开始时间。拆分(“:”)//转换为数组
//取回
var小时数=数量(filteredObject[0]);
var分钟数=数量(filteredObject[1]);
var秒数=数字(filteredObject[2]);
//算计
var时间值;
如果(小时数>0&&12小时){
timeValue=“”+(小时-12);
}否则,如果(小时==0){
timeValue=“12”;
}
时间值+=分钟<10?“:0”+分钟:”:“+分钟;//获得会议记录
时间值+=秒<10?“:0”+秒:”:“+秒;//获得秒数
时间值+=小时>=12?“P.M.”:“A.M.”;//获取上午/下午
//展示
console.log(timeValue);

当我运行此代码时,它不会显示输出。我不知道为什么它不能从CLI显示,CLI代表命令行界面。我希望您能够提供帮助并指出我遗漏或添加了一些功能的地方。

您可以使用
filteredObject[0]。会议[0]。开始时间。split(“:”)
拆分:
但是您的
filteredObject
开始时间“:“1730h”
-否
拆分
返回一个数组,它不会更改变量。您需要将结果影响到一个新变量。在您的对象中,时间的形式为
HHmm\h
,但您的代码期望的形式为
HH:mm:ss
。。。