如何将输入类型时间的值传递给Date对象?

时间:2015-08-21 07:51:33

标签: javascript html html5 time

此函数将时间转换为12小时格式,并为此函数的Stack Overflow贡献一个贡献者:

JS

function ampm(date) {
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // 0 should be 12
    minutes = minutes < 10 ? '0'+minutes : minutes; // if minutes less than 10, add a 0 in front of it ie: 6:6 -> 6:06
    var strTime = hours + ':' + minutes + ' ' + ampm;
    document.getElementById('time').value = strTime;
    return strTime;
}

////This is how the value of the time input is supposed to be displayed in 12 hr format
_("display_time").innerHTML = ampm(new Date());

HTML

<!--This is the input field where a user selects a time-->
<input id="time" placeholder="Time" type="time" name="time" />




<!--This is where the value of the time input is supposed to be displayed in 12 hr format-->>
<span id="display_time"></span> 

我的问题是如何获取时间输入字段的值以12hr格式显示在span标记上。此代码是半工作的。

以12小时格式显示时间,但仅显示当前时间。流程图就像是,用户选择时间输入 - &gt;一些JS转换为12hr格式 - &gt;在span标签中显示为12hr格式。有什么建议或意见吗?

3 个答案:

答案 0 :(得分:1)

您的输入值将是字符串,而不是日期。我已经设置了jsfiddle,我已经修改了你的javascript来处理字符串。

$('#time').on('change', function() {
    var date = $('#time').val().split(':');

    var hours = date[0];
    var minutes = date[1];

    $('#display_time').text(ampm(hours, minutes));
});

function ampm(hours, minutes) {
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12 || 12;
    minutes = minutes || 0;
    minutes = minutes < 10 ? '0'+minutes : minutes;
    return hours + ':' + minutes + ' ' + ampm;
}

答案 1 :(得分:1)

这样的事情会做到这一点

function showTime() {
    //Grab the time and split into hrs and mins
    var time = document.getElementById("time");
    if ( time.value === "")  {
        document.getElementById("mySpan").innerHTML = "";
        return false;
    }
    var hrs = time.value.split(":")[0];
    var mins = time.value.split(":")[1];
    var newTime = ampm(hrs, mins);
    document.getElementById("mySpan").innerHTML = newTime;
}

function ampm(hrs, mins) {
    return ( hrs % 12 || 12 ) + ":" + mins + (( hrs >= 12 ) ? "PM" : "AM" );
}

Here就是一个例子。只需要在时间输入的更改上运行showTime()

答案 2 :(得分:1)

没有必要使用Date及其方法输入为String,因此您最好使用.split(":")方法,您将获得hoursminutes直接估算值。

然后只测试他们的值是否低于10添加前导0,如果hours高于12使用pm后缀或使用否则am

这是一个实时DEMO,使用时间输入的onchange事件,其值为参数onchange="ampm(this.value)

&#13;
&#13;
function ampm(time) {

  console.log(time);
  if (time.value !== "") {
    var hours = time.split(":")[0];
    var minutes = time.split(":")[1];
    var suffix = hours >= 12 ? "pm" : "am";
    hours = hours % 12 || 12;
    hours = hours < 10 ? "0" + hours : hours;

    var displayTime = hours + ":" + minutes + " " + suffix;
    document.getElementById("display_time").innerHTML = displayTime;
  }
}
&#13;
<!--This is the input field where a user selects a time-->
<input id="time" placeholder="Time" type="time" name="time" onchange="ampm(this.value)" />
<span id="display_time"></span>
&#13;
&#13;
&#13;