moe moe - 1 year ago 111
ASP.NET (C#) Question

How to add today's date in C#?

I am trying to check if a submitted date is equal to today's date using C#. How can I check to see if the submitted date equals today's date. Here is what I have so far:

SqlDataAdapter da = new SqlDataAdapter(@"select distinct UserID, cast (DT_Submitted as date)DT_Submitted from myTable where UserID = @UserID", sqlcon);
DataTable dtSETS = new DataTable();
da.SelectCommand.Parameters.AddWithValue("@UserID", (tempUser));
da.Fill(dtSETS);

if (dtSETS.Rows.Count > 0)
{
DataRow dtSETS_row = dtSETS.Rows[0];
DateTime dtSubmitted = dtSETS_row.Field<DateTime>("DT_Submitted");

if (dtSubmitted == todays date )
{
//do something..
}
}

Answer Source

You can use DateTime.Today property if your DT_Submitted datetime has a midnight as a time part.

Even if not, you can use it's .Date property to compare with it.

if (dtSubmitted.Date == DateTime.Today)
{
    //do something..
}

But of course as Jon says, Today can mean different things than depends on where you are.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download