使用c#将字符串格式日期转换为日期格式 [英] convert the string format date to date format using c#
问题描述
如何使用c sharp将字符串格式日期转换为日期格式。
代码如下;
how to convert string format date into date format using c sharp.
Code as follows;
string[] datecheck;
string dates = "";
datecheck = dates.Split(',');
来自上述日期的字符串格式,我将字符串格式日期转换为该代码的日期格式,如下所示;
from the above date in the string format, i am converting string format date into date format for that code as follows;
string[] datecheck;
string dates = "";
string[] Coursecheck;
datecheck = dates.Split(',');
string dt = Convert.ToDateTime(dates).ToString("ddMMyyyy");
Coursecheck = PKGMinorCode.Split(',');
int len = datecheck.Length;
for (int z = 0; z < len-1; z++)
{
for (int j = z + 1; j < len-1 ; j++)
{
if (Coursecheck[z] == "ARPA" && Coursecheck[j] == "ROC")
{
if (datecheck[z] < datecheck[j])
{
ScriptManager.RegisterStartupScript(this, GetType(), "Invalid date", "alert('ROC Date is greater than the ARPA Date')", true);
return;
}
}
}
}
当我运行上面的代码时显示错误如下;
运算符''<''不能应用于string类型的操作数
上面的错误如下所示:
when i run the above code shows errors as follows;
operator ''<'' cannot be applied to operands of type string
the above error shows in the below line as follows;
if (datecheck[z] < datecheck[j]).
请帮帮我。我上面的代码有什么问题。
问候,
Narasiman P
please help me. what is the problem in my above code.
Regards,
Narasiman P
推荐答案
试试这个
try this
if (Convert.ToDatetime(datecheck[z]) < Convert.ToDatetime(datecheck[j]))
注意如果你在 datecheck [z ]和 datecheck [j]
HappyCoding!
:)
Note this will work if you have proper date value in datecheck[z] and datecheck[j]
HappyCoding!
:)
datecheck []
数组类型是字符串。存储在其中的值为:''2013-03-29'',''2013-03-30''等。你不能用>操作数来比较存储在字符串变量中的日期。你需要将字符串转换为日期才能进行比较。
请阅读这些文章:
DateTime.Parse()方法 [ ^ ]
DateTime.TryParse()方法 [ ^ ]
Convert.ToDateTime()方法 [ ^ ]
datecheck[]
array type is string. Values stored in it are: ''2013-03-29'', ''2013-03-30'' and so on. You can''t use ">" operand to compare dates stored in string variable. You need to convert string to date to enable comparison.
Please, read these articles:
DateTime.Parse() method[^]
DateTime.TryParse() method[^]
Convert.ToDateTime() method[^]
if (Convert.ToDateTime(datecheck[z])<convert.todatetime(datecheck[z]))
{
//do something
}
在类字符串中有一个静态函数比较,例如:
There is a static function Compare in class string ,such as :
if (string.Compare(datecheck[z] , datecheck[j]))
{
//your code
}
这篇关于使用c#将字符串格式日期转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!