Manipulating Dates in C#

  • Thread starter Thread starter Agustin Fernandez
  • Start date Start date
A

Agustin Fernandez

Hi all,
Im trying to achive the next task:

date1 = 10/25/2005;
date2 = 03/28/2006;
DiferenceInWeeks = date2 - date1; // Result must be showed in weeks

Can you help me writing this code?
Thx in advance!
 
Agustin said:
Hi all,
Im trying to achive the next task:

date1 = 10/25/2005;
date2 = 03/28/2006;
DiferenceInWeeks = date2 - date1; // Result must be showed in weeks

Can you help me writing this code?
Thx in advance!

DateTime date1 = new DateTime(2005, 10, 25);
DateTime date2 = new DateTime(2006, 3, 28);
TimeSpan difference = date2.Subtract(date1);

// Gets the number of seven-day periods in the time span:
int sevenDayPeriods = (int)Math.Ceiling(difference.TotalDays / 7);

If you want to know about differences in week numbers (i.e. week of the
year) then you have to mess around with the Calendar class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top