如何在angulajs GET请求中传递日期 [英] How to pass date in angulajs GET request

查看:157
本文介绍了如何在angulajs GET请求中传递日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将angularjs $ http获取请求中的日期字段传递到asp.net web api。请告诉我通过日期字段的正确方法是什么。



我尝试了什么:



下面是我的代码,这里writedate变量有日期值。



AngularJs:

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:



Web Api:

[HttpGet]
        public IHttpActionResult GetMonthDates(DateTime date, bool IsTodayDate)
        {
         .....
        }

推荐答案

http获取进入asp.net web api的请求。请告诉我通过日期字段的正确方法是什么。



我尝试了什么:



下面是我的代码,这里writedate变量有日期值。



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);
http.get(config.apiUrl + "GetMonthDates/" + writedate + "?IsTodayDate=" + IsTodayDate);





Web Api:



Web Api:

[HttpGet]
        public IHttpActionResult GetMonthDates(DateTime date, bool IsTodayDate)
        {
         .....
        }


使用不变格式 yyyy-MM-dd 。由于您在查询字符串中传递了值,因此将使用不变文化对其进行解析:

Melvyn Harbour - MVC ModelBinder和Localization [ ^ ]



如果要包含时间,可以使用 Date.prototype.toJSON() [ ^ ]或 Date.prototype.toISOString() [ ^ ]。



否则,如果你没有现有的日期格式化功能,你可以自己动手:

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:
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 =


这篇关于如何在angulajs GET请求中传递日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆