C# 选择DateTime.Now在Web服务中
我有一个web服务,我正在尝试以下内容 我想现在就进入ProcessReqDate列DateTime.Now(比如2016-11-16 14:32:37.733),但我得到的是这个(2016-11-17 00:00:00.000)C# 选择DateTime.Now在Web服务中,c#,web-services,datetime,C#,Web Services,Datetime,我有一个web服务,我正在尝试以下内容 我想现在就进入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,datetimestyle.None,out-dateField)||
if(!DateTime.TryParseExact(OrderReq.RequestedProcessDate,“yyyy-MM-dd”,CultureInfo.InvariantCulture,datetimestyle.None,out-dateField)||
dateFielddateField)
{
orderInfo.status=“失败”;
orderInfo.order_number=orderNo;
orderInfo.notes=“无效的请求交付日期:”+OrderReq.RequestedProcessDate+“(yyyy-MM-dd)”+Environment.NewLine;
orderFailed=true;
}
其他的
{
order.ProcessDate=DateTime.Today;
}
if(order.ProcessDate==DateTime.Today)
{
order.ProcessReqDate=DateTime.Now;
}
其他的
{
order.ProcessReqDate=DateTime.Today;
}
如果您稍微重写一下代码,那么您就可以更清楚地了解将要发生的事情。您似乎想将订单.ProcessReqDate设置为日期时间。如果没有问题,现在设置为日期时间。如果有问题,今天设置为日期时间
您还可以有效地给出失败的原因,以便用户更容易理解出了什么问题
我不知道您显示的代码的上下文,所以我只是将其放入一个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)
{
}
}
}
使用系统;
利用制度全球化;
命名空间控制台应用程序1
{
班级计划
{
公共枚举OrderInfoStatus
{
失败=0,
成功=1
}
类OrderReq
{
公共字符串RequestedProcessDate{get;set;}
}
类OrderInfo
{
public OrderInfoStatus状态{get;set;}
公共字符串OrderNumber{get;set;}
公共字符串注释{get;set;}
}
阶级秩序
{
public DateTime ProcessDate{get;set;}
public DateTime ProcessReqDate{get;set;}
公共日期时间PODate{get;set;}
}
void X(OrderReq OrderReq、Order Order、OrderInfo OrderInfo、字符串orderNo)
{
日期时间日期字段;
bool isValidRequestedProcessDate=DateTime.TryParseExact(orderReq.RequestedProcessDate,“yyyy-MM-dd”,CultureInfo.InvariantCulture,DateTimeStyles.None,out-dateField);
bool isGoodDate=true;
string problemArea=string.Empty;
如果(!isValidRequestedProcessDate)
{
isGoodDate=false;
problemArea=“无法分析请求的进程日期。”;
}
else if(dateFieldorder.PODate)
{
isGoodDate=false;
problemArea=“请求的处理日期在采购订单日期之后。”;
}
如果(isGoodDate)
{
orderInfo.Status=OrderInfoStatus.Success;
orderInfo.OrderNumber=orderNo;
order.ProcessDate=DateTime.Now;
}
其他的
{
orderInfo.Status=OrderInfoStatus.Failure;
orderInfo.OrderNumber=orderNo;
orderInfo.Notes=“无效的请求交付日期:”+
orderReq.RequestedProcessDate+
“(yyyy-MM-dd)——”+
问题领域+
环境新线;
order.ProcessReqDate=DateTime.Today;
//orderFailed=true;/*请改用orderInfo.Status*/
}
}
静态void Main(字符串[]参数)
{
}
}
}
使用UTC日期时间(例如
DateTime.UtcNow.Date
)代替本地日期时间也是一个好主意。您始终可以将UTC日期时间转换为本地日期时间,但如果区域设置未知,则不可能进行相反的转换。您是否在任何地方都使用日期时间?或者其他数据类型?你有什么问题?为什么你要使用DateTime.Today,如果你想设置DatTime.Now?然后正确地解释你的问题,你的代码不清楚我在寻找这样的选择:如果ProcessReqDate是DateTime,那么ProcessDate=DateTime.Now(日期+时间)或者ProcessDate=DateTime.Today(仅仅是00:00:00的日期+时间)这是什么?DateTime.Now(日期+时间)
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)
{
}
}
}