1

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. Something like this:

new Date(year, time);

how can I do that?

2
  • Have a look here w3schools.com/jsref/jsref_obj_date.asp Commented Feb 11, 2015 at 6:45
  • @mindparse would you please tell me which one of those methods takes two parameters in the form of year and time? Commented Feb 11, 2015 at 6:51

2 Answers 2

3

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.


Note that built-in support for input[type=date] is only available in Angular 1.3.x or later. Earlier versions will not give you access to the date in your model.

Sign up to request clarification or add additional context in comments.

Comments

2

Use the below format.

If hours, minutes, seconds, milliseconds values are not known, give '0' in place of them.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.