Andre Smith
Andre Smith

Reputation: 207

Limiting a DateTime to 10 years before

I want to limit a result to only include records form between Date.Now all the way to 10 years ago.

How do I do this?

 strWhere = WhereAction_DueAfterDate(Date.Now - 10 years)
 strWhere &= " And" & WhereAction_DueBeforeDate(Date.Now)

Upvotes: 1

Views: 71

Answers (2)

Ian
Ian

Reputation: 30813

Use AddYears in DateTime class:

DateTime now As DateTime = DateTime.Now
strWhere = WhereAction_DueAfterDate(now.AddYears(-10))
strWhere &= " And" & WhereAction_DueBeforeDate(now)

It allows you to modify the Now DateTime back by putting - sign.

Side note: declare your DateTime.Now once before you process it further. This is just to avoid little time difference which might happen between the first call and the second call of DateTime.Now if we do otherwise:

strWhere = WhereAction_DueAfterDate(DateTime.Now.AddYears(-10))
strWhere &= " And" & WhereAction_DueBeforeDate(DateTime.Now) 'There is a little difference between the first and the second DateTime Now

Upvotes: 4

sujith karivelil
sujith karivelil

Reputation: 29006

You can use AddYears() method to complete the operation.

strWhere = WhereAction_DueAfterDate(DateTime.Now.AddYears(-10))

Hope that the requirement is for executing a query, if so you can use:

Datecolumn >= DATE_SUB(NOW(),INTERVAL 10 YEAR) // for mysql

Upvotes: 1

Related Questions