check date within last 7 days

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    check date within last 7 days

    How would I check a datetime variable is within the last 7 days?

    *** Sent via Developersdex http://www.developersdex.com ***
  • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

    #2
    Re: check date within last 7 days

    Mike P wrote:
    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.

    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:lasse@vk arlsen.no
    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.

    PGP KeyID: 0xBCDEA2E3

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: check date within last 7 days

      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

      • Duy Lam

        #4
        Re: check date within last 7 days

        Mike P wrote:
        How would I check a datetime variable is within the last 7 days?
        >
        *** Sent via Developersdex http://www.developersdex.com ***
        Maybe the code will be like this:

        public static bool IsDateWithin7Da ys(DateTime dtToCheck) {
        DateTime last7Days = DateTime.Now.Ad dDays(-7);
        return (last7Days <= dtToCheck);
        }

        --
        Thanks,
        Duy Lam Phuong

        Comment

        Working...