如何将日期和时间输入的值组合到一个日期对象?

时间:2014-04-02 17:00:24

标签: javascript html5

我想将时间和日期输入的值组合到一个日期对象中。

    <div id="picker">
        Date: <input type="date" name="date" value="2099-08-01" id="date_"/>
        Time: <input type="time" name="time" value="20:00" id="time_"/>
    </div>
    <script>
        var d = document.getElementById("date_").valueAsDate;
        var t = document.getElementById("time_").valueAsDate
        //?
    </script>

我如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

// create Date object from valid string inputs
var datetime = new Date(date+' '+time);

// format the output
var month = datetime.getMonth()+1;
var day = datetime.getDate();
var year = datetime.getFullYear();

var hour = this.getHours();
if (hour < 10)
    hour = "0"+hour;

var min = this.getMinutes();
if (min < 10)
    min = "0"+min;

var sec = this.getSeconds();
if (sec < 10)
    sec = "0"+sec;

// put it all togeter
var dateTimeString = month+'/'+day+'/'+year+' '+hour+':'+min+':'+sec;

Concatenate a date and time value