我正在尝试将字符串格式化的值转换为格式为dd/MM/yyyy
的日期类型。
this.Text="22/11/2009";
DateTime date = DateTime.Parse(this.Text);
有什么问题?
它有第二个覆盖,要求IFormatProvider
。这是什么? 做我还需要传递这个吗?如果是,如何在这种情况下使用它?
修改
Parse
和ParseExact
之间有什么区别?
修改2
Slaks和Sam的两个答案都在为我工作,目前用户正在提供输入,但我将确保使用maskTextbox确保它们有效。
考虑到类型安全性,性能或您想要的某些方面,哪个答案更好?
答案 0 :(得分:225)
使用DateTime.ParseExact
。
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);
答案 1 :(得分:41)
您需要调用ParseExact
,它会解析与您提供的格式完全匹配的日期。
例如:
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
IFormatProvider
参数指定用于解析日期的区域性
除非您的字符串来自用户,否则您应该通过CultureInfo.InvariantCulture
如果字符串确实来自用户,则应传递CultureInfo.CurrentCulture
,这将使用用户在“控制面板”的“区域选项”中指定的设置。
答案 2 :(得分:20)
解析DateTime的字符串表示是一件棘手的事情,因为不同的文化有不同的日期格式。 .Net知道这些日期格式,并在您致电System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat
时将其从当前文化(DateTime.Parse(this.Text)
)中拉出来;
例如,字符串“22/11/2009”与美国(en-US)的ShortDatePattern不匹配,但它与法国(fr-FR)匹配。
现在,您可以调用DateTime.ParseExact
并传入您期望的确切格式字符串,也可以将适当的文化传递给DateTime.Parse
来解析日期。
例如,这将正确解析您的日期:
DateTime.Parse( "22/11/2009", CultureInfo.CreateSpecificCulture("fr-FR") );
当然,你不应该随意挑选法国,而是选择适合你需要的东西。
您需要弄清楚的是System.Threading.Thread.CurrentThread.CurrentCulture
设置的内容,以及是否/为什么它与您的期望不同。
答案 3 :(得分:15)
虽然上述解决方案有效,但您也可以使用以下内容修改Web配置文件...
<configuration>
<system.web>
<globalization culture="en-GB"/>
</system.web>
</configuration>
参考:Datetime format different on local machine compared to production machine
答案 4 :(得分:10)
您可能需要为该特定日期格式指定文化,如:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); //dd/MM/yyyy
this.Text="22/11/2009";
DateTime date = DateTime.Parse(this.Text);
有关详细信息,请访问:
答案 5 :(得分:3)
基于this reference,下一个方法对我有用:
// e.g. format = "dd/MM/yyyy", dateString = "10/07/2017"
var formatInfo = new DateTimeFormatInfo()
{
ShortDatePattern = format
};
date = Convert.ToDateTime(dateString, formatInfo);
答案 6 :(得分:2)
使用它将字符串转换为datetime:
Datetime DT = DateTime.ParseExact(STRDATE,"dd/MM/yyyy",System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat)
答案 7 :(得分:2)
private DateTime ConvertToDateTime(string strDateTime)
{
DateTime dtFinaldate; string sDateTime;
try { dtFinaldate = Convert.ToDateTime(strDateTime); }
catch (Exception e)
{
string[] sDate = strDateTime.Split('/');
sDateTime = sDate[1] + '/' + sDate[0] + '/' + sDate[2];
dtFinaldate = Convert.ToDateTime(sDateTime);
}
return dtFinaldate;
}
答案 8 :(得分:2)
花了很多时间后我解决了这个问题
string strDate = PreocessDate(data);
string[] dateString = strDate.Split('/');
DateTime enter_date = Convert.ToDateTime(dateString[1]+"/"+dateString[0]+"/"+dateString[2]);
答案 9 :(得分:1)
就像上面提到的那样,您可以将其作为字符串参数发送,但必须具有以下格式:&#39; 20130121&#39;例如,您可以将其转换为直接从控件中获取的格式。因此,您可以从以下文本框中获取它:
date = datetextbox.text; // date is going to be something like: "2013-01-21 12:00:00am"
将其转换为:&#39; 20130121&#39;你使用:
date = date.Substring(6, 4) + date.Substring(3, 2) + date.Substring(0, 2);
以便SQL可以将其转换并将其放入数据库中。
答案 10 :(得分:0)
您也可以使用
this.Text = "22112009";
DateTime newDateTime = new DateTime(Convert.ToInt32(this.Text.Substring(4, 4)), // Year
Convert.ToInt32(this.Text.Substring(2,2)), // Month
Convert.ToInt32(this.Text.Substring(0,2)));// Day
答案 11 :(得分:0)
在代码下面为我工作:
DateTime date = DateTime.Parse(this.Text, CultureInfo.CreateSpecificCulture("fr-FR"));
<强>命名空间强>
using System.Globalization;
答案 12 :(得分:0)
没有外部参考的简单方法:
public string FormatDate(string date, string format)
{
try {
return DateTime.Parse(date).ToString(format);
}
catch (FormatException)
{
return string.Empty;
}
}
答案 13 :(得分:-3)
手动更改:
string s = date.Substring(3, 2) +"/" + date.Substring(0, 2) + "/" + date.Substring(6, 4);
2015年11月22日起,它将于2015年11月22日转换