gpt4 book ai didi

c# - WCF 数据服务 [WebGet] 日期时间参数

转载 作者:行者123 更新时间:2023-11-29 21:11:18 26 4
gpt4 key购买 nike

我尝试将 2 个日期时间参数传递到我的 webget 中,但我不知道如何让它正常工作,我将在下面向您展示我的代码以及我收到的错误,也许现在有人知道这是如何工作的。

[WebGet]
public IQueryable<TestTable> GetCallersByDate(string beginDate, string eindDate)
{
testCDREntities context = this.CurrentDataSource;

DateTime startDt = DateTime.Parse(beginDate);
DateTime endDt = DateTime.Parse(eindDate);



var selectedOrders = from table in context.TestTables
where table.Created >= startDt && table.Created <= endDt
select table;

return selectedOrders;
}

网址:

http://localhost:50088/WebService.svc/GetCallersByDate?beginDate=2016/03/23T20:22:30:14&eindDate=2016/03/2T20:13:11:03

我希望有人能帮助我?

最佳答案

鉴于下面的数据,您应该使用 DateTime.ParseExact 而不是通常的 DateTime.Parse

http://localhost:50088/WebService.svc/GetCallersByDate?beginDate=2016/03/23T20:22:30:14&eindDate=2016/03/2T20:13:11:03

我们可以看到日期字符串的格式是 yyyy/MM/ddTHH:mm:ss 并且 AFAIK 这种格式不是 .NET 原生的

开始日期=2016/03/23T20:22:30:14eindDate=2016/03/2T20:13:11:03

string dateFormat = "yyyy/MM/ddTHH:mm:ss";

DateTime startDt = DateTime.ParseExact(beginDate, dateFormat, CultureInfo.InvariantCulture);
DateTime endDt = DateTime.Parse(eindDate, dateFormat, CultureInfo.InvariantCulture);

关于c# - WCF 数据服务 [WebGet] 日期时间参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36345701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com