在Web服务中选择DateTime.Now

时间:2016-11-16 19:42:04

标签: c# web-services datetime

我有一个网络服务,我正在尝试以下内容。

我想进入ProcessReqDate列DateTime.Now(如2016-11-16 14:32:37.733),但我得到了这个(2016-11-17 00:00:00.000)。

if (!DateTime.TryParseExact(OrderReq.RequestedProcessDate, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateField) ||
    dateField < DateTime.Today ||
    order.PODate > dateField)
{
    orderInfo.status = "Fail";
    orderInfo.order_number = orderNo;
    orderInfo.notes = "Invalid Request Delivery Date: " + OrderReq.RequestedProcessDate + " (yyyy-MM-dd)" + Environment.NewLine;
    orderFailed = true;
}
else
{
    order.ProcessDate = DateTime.Today;
}

if (order.ProcessDate == DateTime.Today)
{
    order.ProcessReqDate = DateTime.Now;
}
else
{
    order.ProcessReqDate = DateTime.Today;
}

1 个答案:

答案 0 :(得分:0)

如果您稍微重写代码,那么您可以更清楚地了解将要发生的事情。 出现,如果没有问题,您要将order.ProcessReqDate设置为DateTime.Now,如果出现问题,请将其设置为DateTime.Today

您还可以有用地说明失败的原因,以便用户更容易理解出错的地方。

我不知道您展示的代码的上下文,所以我只是将其放入void

using System;
using System.Globalization;

namespace ConsoleApplication1
{
    class Program
    {
        public enum OrderInfoStatus
        {
            Failure = 0,
            Success = 1
        }

        class OrderReq
        {
            public string RequestedProcessDate { get; set; }
        }

        class OrderInfo
        {
            public OrderInfoStatus Status { get; set; }
            public string OrderNumber { get; set; }
            public string Notes { get; set; }
        }

        class Order
        {
            public DateTime ProcessDate { get; set; }
            public DateTime ProcessReqDate { get; set; }
            public DateTime PODate { get; set; }
        }

        void X(OrderReq orderReq, Order order, OrderInfo orderInfo, string orderNo)
        {
            DateTime dateField;
            bool isValidRequestedProcessDate = DateTime.TryParseExact(orderReq.RequestedProcessDate, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateField);

            bool isGoodDate = true;
            string problemArea = string.Empty;

            if (!isValidRequestedProcessDate)
            {
                isGoodDate = false;
                problemArea = "Could not parse requested process date.";
            }
            else if (dateField < DateTime.Today)
            {
                isGoodDate = false;
                problemArea = "Requested process date is before today.";
            }
            else if (dateField > order.PODate)
            {
                isGoodDate = false;
                problemArea = "Requested process date is after PO date.";
            }

            if (isGoodDate)
            {
                orderInfo.Status = OrderInfoStatus.Success;
                orderInfo.OrderNumber = orderNo;
                order.ProcessDate = DateTime.Now;
            }
            else
            {
                orderInfo.Status = OrderInfoStatus.Failure;
                orderInfo.OrderNumber = orderNo;
                orderInfo.Notes = "Invalid Request Delivery Date: " +
                    orderReq.RequestedProcessDate +
                    " (yyyy-MM-dd) - " +
                    problemArea +
                    Environment.NewLine;
                order.ProcessReqDate = DateTime.Today;
                //orderFailed = true; /* use orderInfo.Status for this instead */
            }

        }

        static void Main(string[] args)
        {
        }
    }
}

使用UTC日期时间(例如DateTime.UtcNow.Date)而不是本地日期时间也是个好主意。您始终可以将UTC日期时间转换为本地日期时间,但如果区域设置未知,则无法执行相反的操作。