What's a simple way to convert between an am/p

2019-03-11 12:13发布

I've been playing with jquery and I've run myself into a time problem. I've got 3 time input select boxes, one for hours, one for minutes, and one for the Meridian. I need to convert this time into a 24 hour string format and then stick it into a hidden input.

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". Any quick javascript time tips?

5条回答
倾城 Initia
2楼-- · 2019-03-11 12:51
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"

查看更多
该账号已被封号
3楼-- · 2019-03-11 12:52

You should look into the native JavaScript Date Object methods.

查看更多
Juvenile、少年°
4楼-- · 2019-03-11 12:53

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";
查看更多
该账号已被封号
5楼-- · 2019-03-11 12:54

Just to add a plain JS answer, the following will format the time based on the length of the initial string. If h:mm returns HH:mm, if h:mm:ss returns 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.

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

查看更多
男人必须洒脱
6楼-- · 2019-03-11 13:13

Example of tj111 suggestion:

$("#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;  
查看更多
登录 后发表回答