将AM / PM字符串转换为小时

时间:2018-09-25 11:59:54

标签: javascript

我有一个自定义的AM / PM小时数组,我需要将它们转换为24小时并与我的日期字符串一起加入(我需要以以下格式发送到服务器:YYYY-mm-dd hh:mm因此在将am / pm格式化为h)之后,我将加入字符串。这是数组:

time = [
      {
          period: 'AM',
          time: '12:00'
      },
      {
          period: 'AM',
          time: '01:00'
      },
      {
          period: 'AM',
          time: '02:00'
      },
      {
          period: 'AM',
          time: '03:00'
      },
      {
          period: 'AM',
          time: '04:00'
      },
      {
          period: 'AM',
          time: '05:00'
      },
      {
          period: 'AM',
          time: '06:00'
      },
      {
          period: 'AM',
          time: '07:00'
      },
      {
          period: 'AM',
          time: '08:00'
      },
      {
          period: 'AM',
          time: '09:00'
      },
      {
          period: 'AM',
          time: '10:00'
      },
      {
          period: 'AM',
          time: '11:00'
      },
      {
          period: 'PM',
          time: '12:00'
      },
      {
          period: 'PM',
          time: '01:00'
      },
      {
          period: 'PM',
          time: '02:00'
      },
      {
          period: 'PM',
          time: '03:00'
      },
      {
          period: 'PM',
          time: '04:00'
      },
      {
          period: 'PM',
          time: '05:00'
      },
      {
          period: 'PM',
          time: '06:00'
      },
      {
          period: 'PM',
          time: '07:00'
      },
      {
          period: 'PM',
          time: '08:00'
      },
      {
          period: 'PM',
          time: '09:00'
      },
      {
          period: 'PM',
          time: '10:00'
      },
      {
          period: 'PM',
          time: '11:00'
      }
  ];

我没有尝试任何操作,因此无法提供示例代码,也不知道在哪里使用。有一些简单的功能可以做到这一点吗?我没有使用moment.js或任何东西,只是一个带有for循环的自定义下拉菜单,用于从该数组写入时间。

3 个答案:

答案 0 :(得分:2)

遍历每个array(time)元素。结果,您可以以24小时格式添加时间之前的日期。

function convertTimeFormat(time){
    if(time.period === 'AM'){
        return time.time;   
    } 
    else {
        var t = parseInt(time.time.split(":")[0], 10) + 12;
        return t.toString() + ":" + time.time.split(":")[1];
    }
}

for (var i=0; i < time.length; i++) {
   console.log(convertTimeFormat(time[i]));
}

答案 1 :(得分:1)

一种快速而肮脏的方法是使用Date对象:

const time = [{
    period: 'AM',
    time: '12:00'
  },
  {
    period: 'AM',
    time: '01:00'
  },
  {
    period: 'AM',
    time: '02:00'
  },
  {
    period: 'AM',
    time: '03:00'
  },
  {
    period: 'AM',
    time: '04:00'
  },
  {
    period: 'AM',
    time: '05:00'
  },
  {
    period: 'AM',
    time: '06:00'
  },
  {
    period: 'AM',
    time: '07:00'
  },
  {
    period: 'AM',
    time: '08:00'
  },
  {
    period: 'AM',
    time: '09:00'
  },
  {
    period: 'AM',
    time: '10:00'
  },
  {
    period: 'AM',
    time: '11:00'
  },
  {
    period: 'PM',
    time: '12:00'
  },
  {
    period: 'PM',
    time: '01:00'
  },
  {
    period: 'PM',
    time: '02:00'
  },
  {
    period: 'PM',
    time: '03:00'
  },
  {
    period: 'PM',
    time: '04:00'
  },
  {
    period: 'PM',
    time: '05:00'
  },
  {
    period: 'PM',
    time: '06:00'
  },
  {
    period: 'PM',
    time: '07:00'
  },
  {
    period: 'PM',
    time: '08:00'
  },
  {
    period: 'PM',
    time: '09:00'
  },
  {
    period: 'PM',
    time: '10:00'
  },
  {
    period: 'PM',
    time: '11:00'
  }
];

function convertTo24HourFormat(timeObject) {

  if (timeObject.period === 'PM') {

    // Create a date objct and add 12 hours.
    let date = new Date(`1970-01-01T${timeObject.time}:00`);
    date.setHours(date.getHours() + 12);

    // Cut out the time string
    return date.toTimeString().substring(0, 5);
  } else {

    // Return the time as it is.
    return timeObject.time;
  }
}

time.forEach(timeObject => console.log(`${timeObject.time} ${timeObject.period} > ${convertTo24HourFormat(timeObject)}`));

答案 2 :(得分:0)

迭代数组,并在每个元素上使用以下函数将12Hour转换为24Hour

给出如下参数: 格式=“ 24”,str = 1:00 PM(持续时间和时间)

function ConvertTimeformat(format, str) {
    var time = $("#starttime").val();
    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);
}

您可以在这里参考:http://jsfiddle.net/L2y2d/1/