[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

System.DateTime remove (day/week/month)

Posted on 2006-04-17
5
Medium Priority
?
1,740 Views
Last Modified: 2010-05-18
Hello.. how can i remove x days or x weeks or x month from a DateTime.
I saw AddDays, AddMonths.. but i can't find how to substract a specific number of day/week or month from a date.

The only workaround a found is something like that:
System.DateTime sysDt = System.DateTime.Now()
sysDt = System.DateTime.FromOADate(Convert.ToDouble(sysDt.DayOfYear - 2));

I've not test well. and i want to know if it exist any easier way to do that.
Thanks for your help..
Alex..
0
Comment
Question by:jabcoco
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
GENTP earned 90 total points
ID: 16472495
Just put AddDays(-1) or AddMonths(-1) to subtract one day or month.
0
 
LVL 10

Author Comment

by:jabcoco
ID: 16472671
Doh!
Thanks.
0
 
LVL 10

Author Comment

by:jabcoco
ID: 16472685
hum.. i will accept question later... i may have other question about remove/add week...
0
 
LVL 6

Assisted Solution

by:PoeticAudio
PoeticAudio earned 60 total points
ID: 16473369
You can also use TimeSpan....

I don't have the IDE up right now (probably errors), but here's some code that might lead you in the right direction...

public DateTime SubtractDaysFromDate(DateTime dt, int numDays)
{
    TimeSpan span = new TimeSpan(numDays, 0, 0, 0, 0);
     
     DateTime date = dt.Subtract(span);
     return date;
}


usage:
DateTime dt = DateTime.Now;
dt = SubtractDaysFromDate(dt, 5);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question