Re: DateTime manipulation

Tech-Archive recommends: Fix windows errors by optimizing your registry



Nicholas Paldino [.NET/C# MVP] <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>     I think the problem here is that you have the time the store is open as 
> a time, and not really a date.  You don't store every date that the store is 
> open, just the general hours.  This information is in a TimeSpan.
> 
>     Then, you have the current date.  What I think you should do is create 
> the current date the store opens for the day you are comparing.  So, assume 
> you have the time the store opens in a TimeSpan, like so:
> 
> TimeSpan storeOpen;
> 
>     And then you get the current date.
> 
> DateTime now = DateTime.Now;
> 
>     Once you have that, you just need to know when the store opens today:
> 
> DateTime storeOpensToday = (now - now.TimeOfDay) + storeOpen;
> 
>     Then, you can compare that against now:
> 
> if (now >= storeOpensToday)
> {
>     // Perform your work.
> }

Note that it's important to only take DateTime.Now *once* (as Nick has 
done above). If you use it twice, it's possible that the day will 
change between calls!

I'd use now.Date rather than (now - now.TimeOfDay) though.

-- 
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: OT- Shopping in hard economic times just got harder
    ... figured walmart took it back from someone else and 'they' sealed it up ... the store and make sure it is what should be inside. ... My friend in China says everyone there *always* opens anything they're ... I'm opening EVERYTHING from here on out.. ...
    (alt.home.repair)
  • Re: OT- Shopping in hard economic times just got harder
    ... just figured walmart took it back from someone else and 'they' ... leave the store and make sure it is what should be inside. ... My friend in China says everyone there *always* opens anything ... I'm opening EVERYTHING from here on out.. ...
    (alt.home.repair)
  • Re: Form "relations"
    ... 3/ Do I want to store anything else with this, user name, ... to contain the calling forms name just after the calling ... opens the new form. ... parent/child relationship, something like ...
    (microsoft.public.access.formscoding)
  • Re: OT- Shopping in hard economic times just got harder
    ... I'm going to have the store open the box or I'm going to ... Musta been an inside job that someone had the tools to shrink-wrap the ... A customer at the store opens something to read the ... It's fair to reshrink it if that's all that happened, ...
    (alt.home.repair)
  • Re: Adding images to a screen and db.
    ... If you don't really need to store the image in a db, ... and have it show the image when the user opens a screen or ... icon when I open the screen if the image is a gif, jpeg. ... it is a bmp or ico it works fine. ...
    (microsoft.public.fox.programmer.exchange)