新日期()12/24小时格式JavaScript?

时间:2016-09-14 13:43:30

标签: javascript date

我有一个简单的验证,可以防止输入/输出错误值。因此,如果用户选择例如:

12:30 PM = Time In12:15 PM = Time Out

他们将在屏幕'Time Out can not be less or equal than Time In'上收到消息。

我的验证工作正常,但在一个案例中。例如用户选择:

Time In: 12:30 PMTime Out: 1:00 PM

他们点击保存并提示消息弹出窗口。我发现我的代码产生了这样的东西:

Time In: Thu Sep 15 2016 00:29:00 GMT-0500 (Central Standard Time)Time Out: Wed Sep 14 2016 13:29:00 GMT-0500 (Central Standard Time)

我使用JavaScript为Time In和Time out值创建新的Date。看起来我的代码在new Date()中的12 PM小时设置为00。所以我的问题是如何防止这种情况或在JavaScript中有任何其他方法来解决这个问题。这是我的代码,我在转换为新的Date()时输入两个值:

var timeIn = document.getElementById('timeIn').value;
var timeOut = document.getElementById('timeOut').value;
var today = new Date(),
  curYear = today.getFullYear(),
  curDate = today.getDate(),
  curMonth = today.getMonth(),

  defaultTimeIn = timeIn.split(":"),
  defaultTimeOut = timeOut.split(":"),

  defaultHourIn = parseInt(defaultTimeIn[0], 10),
  defaultHourOut = parseInt(defaultTimeOut[0], 10),

  defaultMinAMPMIn = defaultTimeIn[1].split(" "),
  defaultMinAMPMOut = defaultTimeOut[1].split(" "),

  defaultMinIn = defaultMinAMPMIn[0],
  defaultMinOut = defaultMinAMPMOut[0],

  defaultAMPMIn = defaultMinAMPMIn[1],
  defaultAMPMOut = defaultMinAMPMOut[1];

if (defaultAMPMIn == "PM") {
  defaultHourIn += 12; //hours are in 24-hour format (0-23)
}

if (defaultAMPMOut == "PM") {
  defaultHourOut += 12; //hours are in 24-hour format (0-23)
}

var compTimeIn = new Date(curYear, curMonth, curDate, defaultHourIn, defaultMinIn);
var compTimeOut = new Date(curYear, curMonth, curDate, defaultHourOut, defaultMinOut);

if (compTimeIn >= compTimeOut) {
  alert("'Time Out' can't be less or equal to 'Time In'");
}

4 个答案:

答案 0 :(得分:2)

12小时制到24小时制,小时+期间转换。

function to24Hour(hour, period) {
  if (period === 'AM' && hour === 12) {
    hour -= 12;
  } else if (period === 'PM' && hour < 12) {
    hour += 12;
  }
  return hour;
}
const hours12 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const hours24 = [];
for (let hour of hours12) {
  hours24.push(to24Hour(hour, 'AM'));
  hours24.push(to24Hour(hour, 'PM'));
}
console.log(hours24);

答案 1 :(得分:1)

只需改变你的状况:

defaultAMPMOut/In == "PM"

defaultAMPMOut/In == "PM" && defaultHourOut/In != 11 //or 12 depending on if it starts at 0 or 1

发生的事情是,如果它是12,那么它仍然会在不应该的时候添加12,将其翻到0。

修改

如果是12 AM,你还必须减去12小时,所以它是0

if (defaultAMPMIn == "PM" && defaultHourIn != 12) {
  defaultHourIn += 12; //hours are in 24-hour format (0-23)
}

if (defaultAMPMOut == "PM" && defaultHourOut != 12) {
  defaultHourOut += 12; //hours are in 24-hour format (0-23)
}

if (defaultAMPMOut == "AM" && defaultHourOut == 12) {
  defaultHourOut -= 12; //hours are in 24-hour format (0-23)
}

if (defaultAMPMIn == "AM" && defaultHourIn == 12) {
  defaultHourIn -= 12; //hours are in 24-hour format (0-23)
}

答案 2 :(得分:1)

为什么不做一些更简单的事情并将其转换为可重复使用的功能,例如:

var time = document.getElementById('timeIn').value;
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);

答案 3 :(得分:-1)

试试这个。

  // Your Code
    if (defaultAMPMIn == "PM" && defaultHourIn >12) {
                defaultHourIn += 12;//hours are in 24-hour format (0-23)
            }

            if (defaultAMPMOut == "PM" && defaultHourOut >12) {
                defaultHourOut += 12;//hours are in 24-hour format (0-23)
            }
   //Your Code