Ajay
Ajay

Reputation: 317

How Do I take time in current format in console App

I have a console app where I need to compare date and time and need to fire emails.

To compare dates:

if ((String.Format("{0:dd/MM/yyyy}", thisresult.schedule_StartDate)) == String.Format("{0:dd/MM/yyyy}", DateTime.Now.ToShortDateString())

It is working fine. But Now I need to put && condition here and compare time also. In database I have time stored as nvarchar. I have data like this stored in database: 10 : 00.

For this I tried to do like below:

if ((String.Format("{0:mm}", thisresult.schedule_starttime)) == String.Format("{0:mm}", DateTime.Now.ToShortDateString()))

But this condition is not working. So how can I take time in specified format?

Upvotes: 0

Views: 127

Answers (2)

Sameer
Sameer

Reputation: 2171

you said that you have data like this stored in database: 10 : 00 then try

if (thisresult.schedule_starttime.Replace(" ","") == DateTime.Now.ToString("HH:mm"))

Replace only works if schedule_starttime is string . As you said schedule_starttime is varchar, if not then first convert to string and then compare.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460098

Compare DateTimes not Strings.

To compare the DateTime use DateTime.Today:

thisresult.schedule_StartDate == DateTime.Today

for the TimeSpan use DateTime.TimeOfDay:

thisresult.schedule_starttime == DateTime.Now.TimeOfDay

Upvotes: 2

Related Questions