Welcome to Dream.In.Code
Become a C# Expert!

Join 136,896 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,729 people online right now. Registration is fast and FREE... Join Now!




Returning the date a week ago

 
Reply to this topicStart new topic

Returning the date a week ago, Returning the date a week ago

Rocket452
3 Apr, 2008 - 11:46 AM
Post #1

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 10

I am trying to return the date a week ago from today.

I am using:
CODE
System.DateTime.Now.ToShortDateString()


to return the current date, so basically I want to return that - 7 days so that I can use the result as a parameter in a SQL query.

I know that System.DateTime has a Subtract method but I dont think it will serve my purposes.

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Returning The Date A Week Ago
3 Apr, 2008 - 11:59 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
And why won't the subtract method work? Looks like it works to me...

csharp

// Get the time now
System.DateTime theTime = System.DateTime.Now;

// Create a time span object for 7 days
System.TimeSpan timespan = new TimeSpan(7,0,0,0);

// Subtract the span from the time
theTime = theTime.Subtract(timespan);

// Messagebox now shows time 7 days ago
MessageBox.Show(theTime.ToString());


Then you can do what you will with the time object there in your query.

smile.gif
User is online!Profile CardPM
+Quote Post

Rocket452
RE: Returning The Date A Week Ago
3 Apr, 2008 - 01:25 PM
Post #3

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 10

Thank you this works well.

I assumed the timeSpan object was a specific date so that why I didnt use it.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Returning The Date A Week Ago
3 Apr, 2008 - 01:35 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Timespan represents well... a span of time. In our example it represents the span of 7 days. smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 09:25PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month