How would I check a datetime variable is within the last 7 days?
>
*** Sent via Developersdex http://www.developersdex.com ***
DateTime dt = ...; // the one you want to check
if (dt >= DateTime.Now.Ad dDays(-7))
{
}
this will rewind the clock 7 days back and check if "dt" is that point
in time, or after it.
However, if the current time of day is mid-day (12:00), and you want the
dt to be in the last 7 "days", where you include the whole day, then you
need to first get a DateTime value within that day 7 days back, and then
rewind the time to midnight.
Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.
On May 2, 10:21 am, Lasse Vågsæther Karlsen <la...@vkarlsen .nowrote:
Mike P wrote:
How would I check a datetime variable is within the last 7 days?
>
*** Sent via Developersdexht tp://www.developersd ex.com***
>
DateTime dt = ...; // the one you want to check
>
if (dt >= DateTime.Now.Ad dDays(-7))
{
>
}
>
this will rewind the clock 7 days back and check if "dt" is that point
in time, or after it.
>
However, if the current time of day is mid-day (12:00), and you want the
dt to be in the last 7 "days", where you include the whole day, then you
need to first get a DateTime value within that day 7 days back, and then
rewind the time to midnight.
>
DateTime dt7 = DateTime.Now.Ad dDays(-7);
dt7 = new DateTime(dt7.Ye ar, dt7.Month, dt7.Day, 0, 0, 0);
if (dt >= dt7)
{
>
}
>
--
Lasse Vågsæther Karlsen
mailto:la...@vk arlsen.nohttp://presentationmod e.blogspot.com/
PGP KeyID: 0xBCDEA2E3
I think that you could do the same using DateTime.Today instead of
DateTime.Now
Comment