简体   繁体   English

如何将 12 小时格式的字符串时间转换为 integer 然后返回

[英]How to Convert string time in 12-Hr format to integer then back

I want to convert a 12-hour format time into a string, add some minutes, then convert back to string.我想将 12 小时格式时间转换为字符串,添加一些分钟,然后转换回字符串。 My user will be entering a time as a sting example 8:03 AM this time needs to converted into an integer with 9 minutes added to it then converted back to the same 12-hour format just 9 minutes into the future.我的用户将输入一个时间作为刺痛示例 8:03 AM 这次需要转换为 integer 并添加 9 分钟,然后在未来 9 分钟后转换回相同的 12 小时格式。

I've tried to make a Date obj using the string time, but when I do the Date constructor says I am using an invalid date.我尝试使用字符串时间制作 Date obj,但是当我执行 Date 构造函数时说我使用的日期无效。

new Date("8:03 AM")

What I tried after that was adding a speicifc date to the time之后我尝试的是在时间上添加一个特定的日期

new Date("09/10/2022 8:03 AM")

That worked to create a Date obj and then I would try to get just the time using.getTime() and pass that into my intToHHMM function.这可以创建一个 Date obj,然后我会尝试使用.getTime() 获取时间并将其传递给我的 intToHHMM function。

const intToHHMM = function (time) {
    var sec_num = parseInt(time, 10); // don't forget the second param
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours+':'+minutes;
}

But I am getting a weird return that definitely is not hh:mm format.但我得到了一个奇怪的回报,绝对不是 hh:mm 格式。

Can someone lend a hand, I appreciate the help in advance!有人可以伸出援助之手,我提前感谢您的帮助!

Just check this out.看看这个。

 var date = new Date("09/10/2022 8:03 AM"); var options = { hour: 'numeric', minute: 'numeric', hour12: true }; var timeString = date.toLocaleString('en-US', options); console.log(timeString); function addMinutes(date, minutes) { return new Date(date.getTime() + minutes*60000); } var timePlusNineMinString=addMinutes(date,9).toLocaleString('en-US', options); console.log(timePlusNineMinString);

check this function, pass any time and it will add minsToAdd minutes to start .检查这个 function,通过任何时间,它会添加minsToAdd分钟start Correct any mistakes when see them.看到它们时纠正任何错误。

 function addMins(start, minsToAdd) { let am_pm = start.split(" ")[1] let [hr, min] = start.split(" ")[0].split(":") let future = "" min = parseInt(min) + minsToAdd if (min > 59) { min = min - 60 hr = parseInt(hr) + 1 am_pm = hr > 11? am_pm === "PM"? "AM": "PM": am_pm === "AM"? "PM": "AM"; hr = hr > 12? hr - 12: hr; future = [ `${parseInt(hr)}`.length === 1? `0${hr}`: `${hr}`, `${min}`.length === 1? `0${min}`: `${min}`, am_pm ].join(":") } else { future = [`${hr}`.length === 1? `0${hr}`: `${hr}`, `${min}`.length === 1? `0${min}`: `${min}`, am_pm ].join(":") } return future } console.log(addMins("11:56 AM", 9)) //12:05:PM console.log(addMins("11:56 PM", 9)) //12:05:AM console.log(addMins("19:03 PM", 9)) //19:12:PM

You don't need to involve Date methods here, just some magic:您不需要在这里涉及 Date 方法,只需一些魔术:

 const add9 = HM12 => { let [H, M, ampm] = HM12.split(/\W/); let minutes = (H % 12 + 12 * (ampm === 'PM')) * 60 - -M; minutes += 9; // magic M = (minutes % 60 + '').padStart(2, 0); H = minutes / 60 % 24|0; ampm = H < 12? 'AM': 'PM'; H = (H % 12 + '' || '12').padStart(2, 0); return `${H}:${M} ${ampm}`; } console.log('08:03 AM --> ' + add9('08:03 AM')) console.log('11:53 PM --> ' + add9('11:53 PM')) console.log('11:53 AM --> ' + add9('11:53 AM'))

Generally I can say, you can use momentjs .一般来说,我可以说,你可以使用momentjs but if you can share the part of your code there might be better and more accurate solutions.但是如果您可以共享代码的一部分,则可能会有更好,更准确的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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