Thisara Bandara
Thisara Bandara

Reputation: 41

Datepicker value into string in Angularjs

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);

Upvotes: 0

Views: 238

Answers (2)

Risalat Zaman
Risalat Zaman

Reputation: 1337

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>

Upvotes: 1

Ankur
Ankur

Reputation: 3199

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

Here is working stackblitz.

Upvotes: 0

Related Questions