Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to pass date field in angularjs $http Get request into asp.net web api. Please suggest me that what is the correct way to pass date field.

What I have tried:

Below is my code, Here writedate variable has date value.

AngularJs:
var response = $http.get(config.apiUrl + "GetMonthDates/" + writedate + "?IsTodayDate=" + IsTodayDate);


Web Api:
[HttpGet]
        public IHttpActionResult GetMonthDates(DateTime date, bool IsTodayDate)
        {
         .....
        }
Posted
Updated 15-Aug-16 5:33am

1 solution

Use the invariant format yyyy-MM-dd. Since you're passing the value in the query-string, it will be parsed using the invariant culture:
Melvyn Harbour - MVC ModelBinder and Localization[^]

If you want to include the time, you could use either Date.prototype.toJSON()[^] or Date.prototype.toISOString()[^].

Otherwise, if you don't have an existing date-formatting function, you can roll your own:
JavaScript
if (!Date.prototype.toISODateString) {
  (function() {

    function pad(number) {
      if (number < 10) {
        return '0' + number;
      }
      return number;
    }

    Date.prototype.toISODateString = function() {
      return this.getUTCFullYear() +
        '-' + pad(this.getUTCMonth() + 1) +
        '-' + pad(this.getUTCDate());
    };
  }());
}

...

var response = $http.get(config.apiUrl + "GetMonthDates/" + writedate.toISODateString() + "?IsTodayDate=" + IsTodayDate);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900