1

Good day everyone. I'm new to C# but I can't seem to understand how DateTime work. All I wanted to do is to check If a (givenday) = today and time is 7pm I wanted to return true. Is this the right way to do it?

Take note ActionDate is a field which is inputed by the user.

DateTime dateA = Convert.ToDateTime(ActionDate);

int a = dateA.Year;
int b = dateA.Month;
int c = dateA.Day;
int d = timeA.Hour;
int e = timeA.Minute;

var newDate = new DateTime(a, b, c, d, e, 0);

DateTime end = Convert.ToDateTime(newDate);
DateTime start = Convert.ToDateTime(A);

TimeSpan span = end.Subtract(start);
Decimal minutes = Convert.ToDecimal(span.TotalMinutes);

if 
{
return true;
} else
{
return false;
}

Thank you in advance.

Ian Jeff
  • 25
  • 5

3 Answers3

1

The way to check if a give date is today and is at 7pm is to use DateTime.Now.

Note that 19 is 7pm and 7 is 7am, the Hour property uses 24 hour format.

bool IsCurrentDayAnd7(DateTime dt) => dt.Date == DateTime.Now.Date && dt.Hour == 19;

As @TimSchmelter commented you could use DateTime.Today:

bool IsCurrentDayAnd7(DateTime dt) => dt.Date == DateTime.Today && dt.Hour == 19;
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
0

You have made your code a bit too complicated. First, convert that user input to date, and compate it with current date and time.

DateTime dateA = Convert.ToDateTime(ActionDate);
if (dateA.Date == DateTime.Today && dateA.Hour == 19)
{
    //it is current date and hour is 7pm
}

Alternatively, check if user's imput is ok, like this:

DateTime dateA;
if (!DateTime.TryParse(ActionDate, out dateA))
{
   //alert user that he's entered wrong date 
}

EDIT: as Tim Schmelter noted, code's a bit more readable using DateTime.Today instead of DateTime.Now.Date

Nino
  • 6,931
  • 2
  • 27
  • 42
  • Instead of `DateTime.Now.Date` you can use `DateTime.Today` – Tim Schmelter Feb 01 '17 at 09:16
  • If I wanted to make it as 7pm do i only chage it to 19? or 1900? – Ian Jeff Feb 01 '17 at 09:17
  • 1
    19. I have edited code. If you want to check minutes also, you can do it like this: `if (dateA.Date == DateTime.Now.Date && dateA.Hour == 19 && dateA.Minute == 0)` – Nino Feb 01 '17 at 09:22
  • If you want to check the range of Hour properties, the easiest way is to just put your cursor on it, press F1, the MSDN is quite comprehensive. – Martheen Feb 01 '17 at 09:23
0

You can use Date property to compare date with current date.

if (newDate.Date == DateTime.Now.Date && newDate.Hour == 19)
{
 return true;
}
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
  • Instead of `DateTime.Now.Date` you can use `DateTime.Today` – Tim Schmelter Feb 01 '17 at 09:17
  • @TimSchmelter any performance impact or significant difference b/w two ? – Mairaj Ahmad Feb 01 '17 at 09:17
  • Sure, `Now` and `Date` need to do calculations and create a new instance of `DateTime`. But apart from that it's more readable because you want to compare with `Today` – Tim Schmelter Feb 01 '17 at 09:22
  • @Tim `DateTime.Today` includes hours, minutes and seconds, while `Date` has those values as 0 – Nino Feb 01 '17 at 09:23
  • @TimSchmelter i agree. Thanks for info. – Mairaj Ahmad Feb 01 '17 at 09:23
  • @Nino i know that `Date` would have `hours,minutes` as 0 and of course @TimSchmelter knows that. – Mairaj Ahmad Feb 01 '17 at 09:24
  • @Nino: `DateTime.Today` doesn't "include" hours,minutes,seconds. All are set to 0. So the result is the same as `DateTime.Now.Date` but the intent is clearer and it's also a little bit more efficient to use `Today`. – Tim Schmelter Feb 01 '17 at 09:25
  • @Tim, you're right. I was thinking of `Now`. Sorry. – Nino Feb 01 '17 at 09:27
  • It's probably not more efficient since both the [reflector](http://stackoverflow.com/a/16247837/529282) and [.NET Source](https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/DateTime.cs) shows Today merely call Now.Date. However, the readability and intent is far more important than the single calculation for resetting the time. – Martheen Feb 01 '17 at 09:30
  • @Martheen: true, didn't know – Tim Schmelter Feb 01 '17 at 11:56