简体   繁体   English

什么是在javascript中将上午/下午时间转换为24小时时间的简单方法

[英]What's a simple way to convert between an am/pm time to 24 hour time in javascript

I've been playing with jquery and I've run myself into a time problem. 我一直在玩jquery,我遇到了一个时间问题。 I've got 3 time input select boxes, one for hours, one for minutes, and one for the Meridian. 我有3个时间输入选择框,一个用于小时,一个用于分钟,一个用于Meridian。 I need to convert this time into a 24 hour string format and then stick it into a hidden input. 我需要将此时间转换为24小时字符串格式,然后将其粘贴到隐藏的输入中。

var time = "";
time += $("#EventCloseTimeHour option:selected").text();
time += ":" + $("#EventCloseTimeMin option:selected").text() + ":00";
time += " " + $("#EventCloseTimeMeridian option:selected").text();

$("#ConditionValue").val(time);         

This gives me "1:30:00 pm" where I need "13:30:00". 这给了我“1:30:00 pm”,我需要“13:30:00”。 Any quick javascript time tips? 任何快速的javascript时间提示?

Example of tj111 suggestion: tj111建议示例:

$("#ConditionValue").val(
    (
        new Date(
            "01/01/2000 " + 
            $("#EventCloseTimeHour option:selected").text() +
            $("#EventCloseTimeMin option:selected").text() + ":00" +
            " " +
            $("#EventCloseTimeMeridian option:selected").text()
        )
    ).toTimeString().slice(0,8))
;

Or you can use: 或者您可以使用:

hour = hour %12 + (meridian === "AM"? 0 : 12);
hour = hour < 10 ? "0" + hour : hour;  

您应该查看本机JavaScript Date Object方法。

var time = "";
var hour = Number($("#EventCloseTimeHour option:selected").text())%12 + ($("#EventCloseTimeMeridian option:selected").text() == 'PM'?12:0)) ;


time +=("00"+String(hour)).slice(-2)+":"+ $("#EventCloseTimeMin option:selected").text();

The key to this solution is using the conditional operator "a?b:c" which translates to "if a then b else c" 这个解决方案的关键是使用条件运算符“a?b:c”,它转换为“if a then b else c”

Just to add a plain JS answer, the following will format the time based on the length of the initial string. 只是为了添加一个简单的JS答案,下面将根据初始字符串的长度格式化时间。 If h:mm returns HH:mm, if h:mm:ss returns HH:mm:ss. 如果h:mm返回HH:mm,如果h:mm:ss返回HH:mm:ss。

If HH:mm:ss is always required, it's simple to add default seconds of ":00" if not present in initial string. 如果总是需要HH:mm:ss,如果初始字符串中不存在,则添加“:00”的默认秒是很简单的。

 // Change time in h:mm:ss ap to HH:mm:ss time // If time missing seconds, returns HH:mm function timeToHHmm(time) { var nums = time.match(/\\d+/g); var am = /am/i.test(time); nums[0] = ('0' + ((nums[0]%12) + (am? 0 : 12))).slice(-2); // If HH:mm:ss required, use following line instead of last // return nums.join(':') + (nums.length == 2? ':00' : ''); return nums.join(':'); } // Some tests ['2:45am', // hh:mm only '5:15:45pm', // hh:mm:ss '5:15:45 pm', // space before am/pm '05:15:45 pm',// leading zero on hour '12:02am', '12:02 pm', '12:02:00am' ].forEach(function(time) { console.log(time + ' => ' + timeToHHmm(time)); }); 

I got it with a few people's answers. 我得到了一些人的答案。

var time = "";
var hour = Number($("#EventCloseTimeHour option:selected").text());
if($("#EventCloseTimeMeridian option:selected").text() == 'PM'){
    hour = String((hour%12) + 12);
}
hour = hour < 10 ? "0" + hour : hour;
time +=hour+":"+ $("#EventCloseTimeMin option:selected").text() + ":00";

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

相关问题 Javascript:将 24 小时时间字符串转换为带有 AM/PM 且无时区的 12 小时时间 - Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone 如何将 am/pm 日期转换为 24 小时时间 - How to convert am/pm date to 24 hour time 12 小时 Javascript 时钟显示 24 时间,错误的 AM/PM - 12 hour Javascript Clock Showing 24 time, Wrong AM/PM 如果用户的机器使用 12 小时制(上午/下午)或 24 小时制(军用时间),则使用 javascript 检测 - Detect with javascript if user's machine is using 12 hour clock (am/pm) or 24 clock (military time) 使用 Javascript 将 24 小时制转换为 12 小时制 - Converting 24 hour time to 12 hour time w/ AM & PM using Javascript 将时间字符串HH:mm am / pm更改为24小时时间 - change time string of HH:mm am/pm to 24 hour time 在Javascript中将时间戳从上午/下午转换为24小时 - Convert timestamp from am/pm to 24-hour in Javascript 将格式时间 AM / PM 更改为 24 小时时间轴日 fullcalendar - Change Format Time AM / PM to 24 hour Timeline Day fullcalendar 支持AM / PM时间格式以及24小时 - support AM/PM time format as well as 24 hour Angular - 有没有办法知道用户的设备时间格式是什么? 24 小时制与 12 点/下午制 - Angular - Is there a way to know what is the user's device time format ? 24h system vs 12 am/pm system
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM