简体   繁体   English

如何从输入日期和输入时间在javascript中获取整个Date对象

[英]how to get whole Date object from input date and input time in javascript

I have these two inputs: 我有两个输入:

<input type="date" ng-model="year">
<input type="time" ng-model="time">

I want to create a Date object from these two values. 我想从这两个值创建一个Date对象。 Something like this: 像这样:

new Date(year, time);

how can I do that? 我怎样才能做到这一点?

You can manually add the two dates together in your controller. 您可以将两个日期手动添加到控制器中。 If you want this to automatically update on your controller scope, then you can use a watch expression in tandem with the date arithmetic. 如果您希望它在控制器范围内自动更新,则可以将监视表达式与日期算术一起使用。 For example: 例如:

$scope.$watchGroup(['year', 'time'], function (values) {
    var year = values[0], time = values[1];
    $scope.date = new Date(year.getYear(), year.getMonth(), year.getDay(), time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
});

Then you can use date on your scope however you wish, and it will automatically update whenever year or time change. 然后,您可以根据需要在date范围内使用date ,并且当yeartime更改时, date将自动更新。


Note that built-in support for input[type=date] is only available in Angular 1.3.x or later. 请注意,仅在Angular 1.3.x或更高版本中提供对input[type=date]内置支持。 Earlier versions will not give you access to the date in your model. 早期版本将无法访问模型中的日期。

Use the below format. 使用以下格式。

If hours, minutes, seconds, milliseconds values are not known, give '0' in place of them. 如果不知道小时,分钟,秒,毫秒的值,则将其替换为“ 0”。

var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

here the variable 'd' will return the date object, from which you get epoch time also. 在这里,变量“ d”将返回日期对象,您还可以从中获取时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM