abhaya
abhaya

Reputation: 31

How to send date as YYYY/mm/dd in get api request rather than long format 2019-12-09T18:15:00.000 in angularjs?

I am making a get api request in angularjs. My date format in api is yyyy/mm/dd but i cant be able to send my ng-model date value in such format. How can this be achieved? Date is only sent as 2019-12-09T18:15:00.000. My input is

<input type="date" ng-model="init" class="form-control">
<input type="date" ng-model="final" class="form-control">

And my api request is:

vm.fetchlist = function () {
    var stockPayload = {
        init:    $scope.init,
        final: '2019/11/14',
    };

    var url = 'http://demo/api/stocksalessummary";
    var config = { params: stockPayload };   

    $http.get(url, config, { headers: { Authorization: 'Bearer ' + token } })
}

I tried sending date format in ng-click event but cant seem to work as i also have 2 date fields to pass

<button class="btn btn-link " ng-click="vm.fetchList(init | date:'yyyy/MM/dd');">GET</button>

Upvotes: 0

Views: 1038

Answers (4)

abhaya
abhaya

Reputation: 31

I did it by

<button class="submit" ng-click="vm.fetchStockSalesSummaryList(init| date : 'yyyy/MM/dd',final | date : 'yyyy/MM/dd');">

Adding this line of filter code every time given function is called..Thannk you

Upvotes: 0

likeToCode
likeToCode

Reputation: 852

I am new to angularjs or js but I want to present a simple solution for your issue and I also tested it. Below is the a simple way to solve your issue, but am sure there should be a clean way to fix this. Please see the code below

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js">
</script>
var myApp = angular.module("app", []);

myApp.controller('controller', function ($scope, $http) {

    $scope.formatDate = function () {


        var year =  $scope.init.getFullYear();
        var month = $scope.init.getMonth()+1;
        var day = $scope.init.getDate();
        var date = year+"/"+month+"/"+day;
        var stockPayload = {

                init:    date,
                final: '2019/11/14',
            };

        alert('The Date is '+ stockPayload.init);
    }
});
<div ng-app="app" ng-controller="controller">

    <input type="date" ng-model="init" class="form-control" datetime="yyyy/MM/dd">
    <input type="button" ng-click="formatDate()" value="Get Date" />

</div>

and it is tested here and works

https://www.tutorialspoint.com/online_angularjs_editor.php

Upvotes: 1

georgeawg
georgeawg

Reputation: 48968

Use the AngularJS date filter to format the date:

vm.fetchlist = function () {
    var stockPayload = {
        init:   $filter("date")($scope.init,"yyyy/MM/dd")
        final: '2019/11/14',
    };

    var url = "http://demo/api/stocksalessummary";
    var config = { 
       params: stockPayload,   
       headers: { Authorization: 'Bearer ' + token } 
    };   

    $http.get(url, config);
}

Also the $http.get request needs to be formed correctly.

For more information, see

Upvotes: 1

Dhruv Parekh
Dhruv Parekh

Reputation: 335

you can iterate your data and format the Date

moment(date).format("YYYY MM DD");

Upvotes: 1

Related Questions