简体   繁体   中英

Format DateTime without converting it to String

I have a GetOrders class that required me to supply the start and end dates as the DateTime values. Nevertheless, when I supply the dates, I am getting this error message: Sorry, the end date was missing, invalid, or before the start date. <EndDate> must be in YYYY-MM-DD or YYYY-MM-DD HH:MI:SS format, and after the start date. Sorry, the end date was missing, invalid, or before the start date. <EndDate> must be in YYYY-MM-DD or YYYY-MM-DD HH:MI:SS format, and after the start date.

This is my code:

ff.GetOrders(DateTime.UtcNow, DateTime.UtcNow.AddMonths(-1), TradingRoleCodeType.Buyer, OrderStatusCodeType.Completed);

When I try to use formatted string, it does not work:

        String dt1 = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
        String dt2 = DateTime.UtcNow.AddMonths(-1).ToString("yyyy-MM-ddTHH:mm:ssZ");

Assuming GetOrders has startdate,enddate (which I can't tell because you didn't give the function prototype for GetOrders), then your dates are in the wrong order or you should AddMonths(1) instead of -1.

(If this isn't the case, please include the function proto for GetOrders and I will revise or delete my answer.)

As noted, your maths are a little...off, I think:

ff.GetOrders( DateTime.UtcNow               ,
              DateTime.UtcNow.AddMonths(-1) ,
              TradingRoleCodeType.Buyer     ,
              OrderStatusCodeType.Completed
            ) ;

Try something like

DateTime dtStart = DateTime.utcNow      ;
DateTime dtEnd   = dtStart.AddMonths(1) ;

ff.GetOrders( dtStart ,
              dtEnd   ,
              TradingRoleCodeType.Buyer ,
              OrderStatuscodeType.Completed
            ) ;

As a general practice, keeping individual computations separate rather than combining them in a method call makes the code easier to understand, easier to modify and easier to debug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM