比较日期时我遇到了问题。 [英] I am getting problem when comparing date .
本文介绍了比较日期时我遇到了问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在localhost上运行时,我的代码工作正常,但是当我上传到服务器时,它会出错...
my code work fine when i run on localhost, but when i upload to server it give error..
String was not recognized as a valid DateTime.
我尝试过:
What I have tried:
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string userid = GVmydsr.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = (GridViewRow)GVmydsr.Rows[e.RowIndex];
Label date = (Label)row.FindControl("lblcurrentdate");
string cdate = date.Text.ToString();
DateTime date1 = DateTime.ParseExact(cdate, "MM/dd/yyyy 00:00:00", null);
string dt1 = date1.ToString("MM/dd/yyyy 00:00:00");
GVmydsr.EditIndex = -1;
string dt2 = DateTime.Today.ToString("MM/dd/yyyy 00:00:00");
if (dt1 == dt2)
{
//
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "Success", "alert('Cannot Edit !!!.');", true);
gvbind();
}
}
推荐答案
使用DateTime.TryParseExact Method(System) [ ^ ]
use DateTime.TryParseExact Method (System)[^]
Label date = (Label)row.FindControl("lblcurrentdate");
string cdate = date.Text;
DateTime date1 = DateTime.MinValue;
DateTime.TryParseExact(cdate.Split(' ')[0], "MM-dd-yyyy", null, System.Globalization.DateTimeStyles.None, out date1);
string dateDB = date1.ToString("MM-dd-yyyy");
string day = DateTime.Today.ToString("MM-dd-yyyy");
if (dateDB == day)
{
////
}
Quote:
我在离线时以这种格式获取日期 - 01-04-2018 00:00:00
但上传后,转换成 - 01/04/2018 00:00:00
I am getting date in this format offline - 01-04-2018 00:00:00
but after uploading it, get convert into - 01/04/2018 00:00:00
如果你的字符串是01- 04-2018
然后使用MM-dd-yyyy
if 01 / 04/2018
然后使用MM / dd / yyyy
这将处理案件
if your string is like "01-04-2018"
then use "MM-dd-yyyy"
if "01/04/2018"
then use "MM/dd/yyyy"
this will handle both the case
DateTime.TryParseExact(cdate.Split(' ')[0], new string[] { "MM-dd-yyyy", "MM/dd/yyyy" }, null, System.Globalization.DateTimeStyles.None, out date1);
这篇关于比较日期时我遇到了问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文