Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
58
rated 0 times [  62] [ 4]  / answers: 1 / hits: 12965  / 11 Years ago, tue, february 4, 2014, 12:00:00

I have two input tags for picking date and time from user.



<p>Start Date</p><p> <input ng-model=sdate type=date ></p>
<p>Start Time</p><p> <input ng-model=stime type=time ></p>


These two values are passed to a function where I want to combine these two input values as a Date object:



 new Date(y, m, d, hh, mm, a)


Which I can then use to plot an event's details in a Calendar. How can I combine these two values? I have tried:



start:new Date(sdate + stime)


start:new Date(sdate , stime)


start: new Date(sdate.getFullYear() + sdate.getMonth() + sdate.getDate() + stime.getHours + stime.getMinutes())


But none of what I have tried is working.



How do I achieve this when using AngularJS?


More From » angularjs

 Answers
4

In angular it would go something like this:



Controller:



function exampleController($scope) {
$scope.title = $Watch sample;

$scope.$watch('sdate', function() {
tryCombineDateTime();
});

$scope.$watch('stime', function() {
tryCombineDateTime();
});

function tryCombineDateTime() {
if($scope.sdate && $scope.stime) {
var dateParts = $scope.sdate.split('-');
var timeParts = $scope.stime.split(':');

if(dateParts && timeParts) {
dateParts[1] -= 1;
$scope.fullDate = new Date(Date.UTC.apply(undefined,dateParts.concat(timeParts))).toISOString();
}
}
}
}


HTML



<div ng-app ng-controller=exampleController>
<h2>{{title}}</h2>
<p>Start Date</p><p> <input ng-model=sdate type=date ></p>
<p>Start Time</p><p> <input ng-model=stime type=time ></p>

{{fullDate}}
</div>


You need to make use of the $watch listener on a variable when it changes, then call your function.



Note: it would be even better if you make a directive for this.



Fidle


[#48079] Monday, February 3, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wyattkennyc

Total Points: 650
Total Questions: 102
Total Answers: 90

Location: Monaco
Member since Mon, May 23, 2022
2 Years ago
;