0

I'm trying to get datepicker value into a string or usable format for passing as a parameter to a get method. However,
the get method i tried always give an error, what is the best way to convert a datepicker value into a string?

<div ng-app="getserviceApp" ng-controller="getserviceCtrl">
        <button ng-click="FunctionLoadData()">Load Data</button>

        <input type="date" ng-model="from_date" />
        <input type="date" ng-model="to_date" />

<script type="text/javascript">

        var app = angular.module('getserviceApp', [])

        app.controller('getserviceCtrl', function ($scope, $http) {

            var FromDate = new Date($scope.from_date.selectedDate);
            var ToDate = new Date($scope.from_date.selectedDate);

2 Answers 2

1

There are some discrepancies in the ng-models and the name of your $scope variables.

In your controller you should have something like

   app.controller('getserviceCtrl', function ($scope, $http) {
     $scope.from_date = new Date();
     $scope.to_date = new Date();

     $scope.loadData = function(){
       console.log($scope.from_date.toISOString().split('T')[0])
       console.log($scope.to_date.toISOString().split('T')[0])
       //prints the dates in yyyy-MM-dd format
     }
   }

And in your HTML

<div ng-app="getserviceApp" ng-controller="getserviceCtrl">
    <button ng-click="loadData()">Load Data</button>

    <input type="date" ng-model="from_date" />
      value = {{from_date | date: "yyyy-MM-dd"}}//Display the date
    <input type="date" ng-model="to_date" />
      value = {{from_date | date: "yyyy-MM-dd"}}
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

I think mistake is from_date.selectedDate. Use just model from_date

Here is working stackblitz.

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.