使用JavaScript将日期和时间字符串合并为单个日期 [英] Combine date and time string into single date with javascript
问题描述
我有一个 datepicker 返回一个日期字符串和一个时间选择器仅返回时间字符串。
I have a datepicker returning a date string, and a timepicker returning just a time string.
我应该如何将它们合并为一个JavaScript日期?
How should I combine those into a single javascript Date?
我以为我在 Date.js 。示例显示了 at()
方法,但是我在库中找不到它...
I thought I found a solution in Date.js. The examples shows an at( )
-method, but I can't find it in the library...
推荐答案
您可以配置日期选择器以返回诸如 YYYY-mm-dd
之类的格式(或 Date.parse
支持),则可以在时间选择器中构建字符串,例如:
You can configure your date picker to return format like YYYY-mm-dd
(or any format that Date.parse
supports) and you could build a string in timepicker like:
var dateStringFromDP = '2013-05-16';
$('#timepicker').timepicker().on('changeTime.timepicker', function(e) {
var timeString = e.time.hour + ':' + e.time.minute + ':00';
var dateObj = new Date(datestringFromDP + ' ' + timeString);
});
javascript Date
对象采用字符串作为构造函数参数
javascript Date
object takes a string as the constructor param
这篇关于使用JavaScript将日期和时间字符串合并为单个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!