473,387 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 ***
Jun 27 '08 #1
3 14898
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.AddDays(-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.AddDays(-7);
dt7 = new DateTime(dt7.Year, dt7.Month, dt7.Day, 0, 0, 0);
if (dt >= dt7)
{
}

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #2
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 Developersdexhttp://www.developersdex.com***

DateTime dt = ...; // the one you want to check

if (dt >= DateTime.Now.AddDays(-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.AddDays(-7);
dt7 = new DateTime(dt7.Year, dt7.Month, dt7.Day, 0, 0, 0);
if (dt >= dt7)
{

}

--
Lasse Vågsæther Karlsen
mailto:la...@vkarlsen.nohttp://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
I think that you could do the same using DateTime.Today instead of
DateTime.Now
Jun 27 '08 #3
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 IsDateWithin7Days(DateTime dtToCheck) {
DateTime last7Days = DateTime.Now.AddDays(-7);
return (last7Days <= dtToCheck);
}

--
Thanks,
Duy Lam Phuong
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ralph Freshour | last post by:
I have a Date type in my MySQL table - I'm trying to do a query on all rows within the last 30 days based on that Date field - I'm having trouble figuring out how to form the query??? $php_SQL =...
5
by: goochey | last post by:
I'm trying to convert a Julian Date (Format "4365") into an actual calendar date in Visual Basic, can anyone help me out with this.
1
by: Teresa | last post by:
I need to set criteria in a query to pull all transfer dates that are between 1 year before and 6 mos before Then I need to pull all service dates that are within six months of that...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
8
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on...
1
by: cptuser | last post by:
Hi, I have a date field on a form. The date in the date field has to be after today's date and not in the past. It also has to be within 30 days from today's date. So if today is 13/04/2007, then...
34
by: -Lost | last post by:
I'm REALLY liking this so far. And for those who welcome something a little less cryptic than what the resident date guru offers, here's a chance to try something fairly elegant and definitely...
0
by: Ignacio Machin ( .NET/ C# MVP ) | last post by:
On May 2, 9:56 am, Mike P <mike.p...@gmail.comwrote: Hi if ( myDateDateTime.Now.AddDays(07)
16
by: W. eWatson | last post by:
Are there some date and time comparison functions that would compare, say, Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd) Is 02/11/07 the same as 02/11/07? Is 14:05:18 after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.