Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateTime.Now: subtract 1 day from it 1

Status
Not open for further replies.

eramgarden

Programmer
Aug 27, 2003
279
US
I have an ASP pages, trying to upgrade it to ASP.Net..

there's a line of code that I cant get right...

It uses "now-1" and "now+1" to find dates...

I tried DateTime.Now.Subtract(1) but didnt work..

How can I subtract 1 from date??
 
You'll have to specify what you want it to subtract. 1 could be anything. You want 1 day.. You could use the TimeSpan class to specify some amount of time. And one of the Subtract methods even takes a TimeSpan as a parameter.. Here's some code:

DateTime yesterday = DateTime.Now.Subtract(TimeSpan.FromDays(1));

 
I've inherited the ASP code....

now-1 means...subtract 1 day..is that correct?
 
Would this work
Code:
DateTime yesterday = DateTime.Now.AddDays(-1)
?

Not tested, but that would be my first guess.

HTH

David
[pipe]
 
Like Dragonwell said -- use the AddDays method to add a negative day.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top