我遇到了DateTime.ParseExtract問題。錯誤是String was not recognized as a valid DateTime
。C#將日期從字符串轉換爲日期格式
sqlserver 2012中的trialDate定義爲folllow type = date,它以這種格式顯示 MM/dd/yyyy。我猜這是sqlserver的默認值。我不知道如何將其更改爲 dd/MM/yyyy或dd-MM-yyyy。
當我顯示在ListView我這樣做的日期(把口罩): '
下面是C#代碼隱藏
string sTrialDate = "";
foreach (ListViewDataItem item in ListView1.Items)
{
CheckBox MyCheckBox = (CheckBox)item.FindControl("MyCheckBox");
if (MyCheckBox.Checked)
{
Label myTrialDate = (Label)item.FindControl("trialDatelbl");
sTrialDate = myTrialDate.Text;
}
}
問題是DATETIME.ParseExtract,這是我到目前爲止所嘗試的:
DateTime dt = DateTime.ParseExact("sTrialDate", "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture); <=== not working
DateTime dt = DateTime.ParseExact("sTrialDate", "dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture); <=== not working
DateTime dt = DateTime.ParseExact("sTrialDate", "dd/MM/yyyy ",
System.Globalization.CultureInfo.InvariantCulture); <=== not working
DateTime dt = DateTime.ParseExact(Request.QueryString["sTrialDate"], "dd/MM/yyyy", null);
<==not working
'「sDateRdv」'看起來並不像一個日期我。 –
我改變了對不起,它應該sTrialDate – user3127986
'「sTrialDate」'也不像日期! –