473,811 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15030
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:la***@vk arlsen.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 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
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 IsDateWithin7Da ys(DateTime dtToCheck) {
DateTime last7Days = DateTime.Now.Ad dDays(-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
7371
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 = "SELECT * FROM basics WHERE creation_date = DATE_ADD(creation_date, INTERVAL -30 DAY)"; ....doesn't do anything... Thanks for any help.
5
16338
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
2827
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 transfer date.. can anyone help.. i've tried a few things and nothing seems to work just right. I tried getting all transfer dates..
7
15510
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 DateDiff in VB.NET which is only available in C# if you don't mind linking in Microsoft.VisualBasic.dll to your C# application. I wanted to avoid this so set about a pure C# solution which uses a combination of TimeSpan in whole days and the...
8
3954
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 this line: Set rs = db.OpenRecordset("SELECT ID FROM Contacts WHERE (" & sWhere & ");") Contacts being the main table. I am using access 2003 The error message states that there are; Too few parameters. Expected 1 I have no idea how to...
1
3722
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 the form can only accept any date within 30 days after the 13/04/2007, so the 14/04/2007 plus 30 days! Can any please help with the correct Javascript code? I'm an amature and don't have a clue on how to achieve this.
34
2540
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 unique. http://www.datejs.com/ -- -Lost Remove the extra words to reply by e-mail. Don't e-mail me. I am kidding. No I am not.
0
896
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
4470
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 22:02:51? (24 hour day is fine) How about the date after 02/28/04 is 02/29/04, or the date after 09/30/08 is 10/01/08?
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10663
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10401
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9217
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.