转换或铸造日期​​字符串日期时间问题时, [英] Issue when converting or casting Date String to DateTime

查看:171
本文介绍了转换或铸造日期​​字符串日期时间问题时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery的DatePicker检索日期插入/更新记录在GridView的一个文本框。在日期选择器的日期被接收为一个字符串。例如值2014年7月31日。

I am using jQuery DatePicker to retrieve date for a textbox in Gridview for inserting/updating records. The datepicker's date is received as a String. e.g. value "07/31/2014".

使用以下code,我将字符串转换日期为C#的DateTime

Using following code I am converting string date into a C# DateTime.

    var sDate = ((TextBox)row.FindControlRecursive("iStartDateTBox")).Text;
    payment.StartDate = DateTime.ParseExact(sDate, "mm/dd/yyyy", null);

然而,一旦转换,设定为支付的开始日期属性的值变为31/01/2014 12时07分00秒
我只是困惑什么是真正发生的事情。并想一些提示,以解决这个问题。

However once converted, the value set to payment's Start Date property becomes "31/01/2014 12:07:00" I am just perplexed about what's really happening. And would like few tips to solve this.

推荐答案

您可以使用DateTime.ParseExact用的IFormatProvider。 ( MSDN

You can use DateTime.ParseExact with IFormatProvider. ( MSDN )

因此​​,尝试这样的:

So try something like this:

var result = DateTime.TryParse(
      "10. 10. 2014", 
      CultureInfo.CreateSpecificCulture("en-US"), 
      DateTimeStyles.None,
      out date);

这篇关于转换或铸造日期​​字符串日期时间问题时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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