1

I am working on a project with AngularJs. Here I have 3 html controls.

  • datepicker (html5's date) => $scope.date
  • timepicker (html5's time) => $scope.from
  • timepicker (html5's time) => $scope.to

below is html code.

<form>
  <div class="form-group row">
    <label for="" class="col-sm-2 col-form-label">Set date</label>
    <div class="col-sm-5">
      <!-- <input type="email" class="form-control" id="inputEmail3" placeholder="Email"> -->
      <datepicker date-format="yyyy-MM-dd" selector="form-control">
        <div class="input-group">
          <input class="form-control" placeholder="Choose a date" ng-model="date" id="date" />
          <spam class="input-group-addon" style="cursor: pointer">
            <i class="fa fa-lg fa-calendar"></i>
          </spam>
        </div>
      </datepicker>
    </div>
  </div>
  <div class="form-group row">
    <label for="" class="col-sm-2 col-form-label">Set time</label>
    <div class="col-sm-4">
      <input type="time" class="form-control" placeholder="From" ng-model="from" id="from">
    </div>
    <label for="" class="col-sm-1 col-form-label">To</label>
    <div class="col-sm-4">
      <input type="time" class="form-control" placeholder="To" ng-model="to" id="to">
    </div>
  </div>
</form>

now for service call i need to pass datetime, but here I'm having date and time separately.

How can I construct datetime from and datetime to from above available data?

2
  • You need to share the HTML and JS, or a working snippet with the problem for debugging the issue! Commented Sep 16, 2017 at 10:20
  • @NarenMurali I have provided HTML. well right now there's no useful JS hence I haven't included it. **Problem is how to construct complete date time from from date and time? for example. datetimeFrom and datetimeTo from data selected by a user. Commented Sep 16, 2017 at 10:37

1 Answer 1

2

You can combine the dates in a function in your controller. First format the dates/times using the Date.prototype.toISOString() method, then use the String.prototype.substring() method to take the datePart and the timePart separately, finally combine the two to create a new Date instance.

$scope.fromDate = getCombinedDateTime($scope.date, $scope.from);
$scope.toDate = getCombinedDateTime($scope.date, $scope.to);

function getCombinedDateTime(date, time){
  var datePart = date.toISOString().substring(0, 10);
  var timePart = time.toISOString().substring(10, 24);
  return new Date(datePart + timePart);
}
Sign up to request clarification or add additional context in comments.

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.