Playing with System.DateTime

  • Thread starter Thread starter Jon Skeet [C# MVP]
  • Start date Start date
J

Jon Skeet [C# MVP]

bredal Jensen said:
I'm building a small booking system and i have come accross quiet a tedious
pitfall.

"I need to make sure that people do not book for tomorrow when todays time
is greater or equal to 11."

Well some of you probably allready know the answer but this is not so
obvious for me.

Can anyone solve this puzzle for me?

The booking hour is of no importance. Only the hour at which we are making
this booking and only if it is the day after today.

if (DateTime.Now.Hour >= 11 && // If it's later than 11am today
// And the booking is after tomorrow midnight
booking >= DateTime.Today.AddDays(1) &&
// And the booking is earlier than 2 days hence midnight
booking < DateTime.Today.AddDays(2))
{
....
}
 
bredal Jensen said:
Great, your code seems to solve my problem.
Ánd i will happily use it.

By the way will the following work?
So far it seems to work. I'just afraid it may break down
for some combination.

if (DateTime.Now.AddHours(13).Day==bookDate.Day) { do not book}

Well, that would cover the case you've described - but it would also
stop someone from booking for 8pm on the same day if they're booking at
7am, for instance.

I would stick with the more explicit version, myself - it's a little
bit clearer in its intentions, in my opinion.
 
well , right , but it should not be possible to book
on the same day. I have just forgotten to add it in the specs.

Maybe because i have already figured
out the logic for excluding same day booking.

Anyway, again thanks, so much.

I must say i have posted this problem on many
news sites and you are the only who provided the correct answer.
Someone suggested a solution that turned out to be wrong

Cheers!




bredal Jensen said:
Great, your code seems to solve my problem.
Ánd i will happily use it.

By the way will the following work?
So far it seems to work. I'just afraid it may break down
for some combination.

if (DateTime.Now.AddHours(13).Day==bookDate.Day) { do not book}

Well, that would cover the case you've described - but it would also
stop someone from booking for 8pm on the same day if they're booking at
7am, for instance.

I would stick with the more explicit version, myself - it's a little
bit clearer in its intentions, in my opinion.
 
bredal Jensen said:
well , right , but it should not be possible to book
on the same day. I have just forgotten to add it in the specs.

Right - I'd make that explicit too then, if I were you.
Maybe because i have already figured
out the logic for excluding same day booking.

Absolutely :)
Anyway, again thanks, so much.

I must say i have posted this problem on many
news sites and you are the only who provided the correct answer.
Someone suggested a solution that turned out to be wrong

My pleasure - glad to help.
 
Hello
The Day properrty is the day of the month, not the date.
So this code will not book if today is 18/6/2004 after 11 and you are
booking for 19/7/2004

Use the date property instead
if (DateTime.Now.AddHours(13).Date==bookDate.Date) { do not book}

But I would use Jon's code since it is more readable

Best regards,
Sherif

bredal Jensen said:
Great, your code seems to solve my problem.
Ánd i will happily use it.


By the way will the following work?
So far it seems to work. I'just afraid it may break down
for some combination.

if (DateTime.Now.AddHours(13).Day==bookDate.Day) { do not book}





Anyway i have a solution, your solution.





Many thanks...
 
You are totally right and i'm already using Jon' code....


Thanks...



Sherif ElMetainy said:
Hello
The Day properrty is the day of the month, not the date.
So this code will not book if today is 18/6/2004 after 11 and you are
booking for 19/7/2004

Use the date property instead
if (DateTime.Now.AddHours(13).Date==bookDate.Date) { do not book}

But I would use Jon's code since it is more readable

Best regards,
Sherif
 
By the way will the following work?
So far it seems to work. I'just afraid it may break down
for some combination.

Then throw lots of borderline cases at it and try to break it.

if (DateTime.Now.AddHours(13).Day==bookDate.Day) { do not book}





Anyway i have a solution, your solution.





Many thanks...
 
Hello gurus,


I'm building a small booking system and i have come accross quiet a tedious
pitfall.

"I need to make sure that people do not book for tomorrow when todays time
is greater or equal to 11."


Well some of you probably allready know the answer but this is not so
obvious for me.

Can anyone solve this puzzle for me?

The booking hour is of no importance. Only the hour at which we are making
this booking
and only if it is the day after today.


Many thanks in advance

J.B
 
Great, your code seems to solve my problem.
Ánd i will happily use it.


By the way will the following work?
So far it seems to work. I'just afraid it may break down
for some combination.

if (DateTime.Now.AddHours(13).Day==bookDate.Day) { do not book}





Anyway i have a solution, your solution.





Many thanks...
 
Back
Top