add timespan to datetime

  • Thread starter Thread starter Lars Schouw
  • Start date Start date
L

Lars Schouw

I would like to to add a time span "1Y 2M 3W 20D" entered as a string
to a date time how do I do that?

Lars
 
Lars Schouw said:
I would like to to add a time span "1Y 2M 3W 20D" entered as a string
to a date time how do I do that?

Well, you'll need to parse the string yourself into a TimeSpan. After
that, however, it's easy:

TimeSpan x = ...;
DateTime y = ...;

DateTime total = y+x;
 
I just read the documentaion quickly does TimeSpan also work with
years, months, weeks?
I can find days.
Lars
 
I just read the documentaion quickly does TimeSpan also work with
years, months, weeks?
I can find days.

Months and years vary in length, so you'll need to work out exactly
what you mean. If you want to add 1 year to Jan 1st and always get Jan
1st, you may want to look at adding each field individually rather
than using TimeSpan: DateTime has AddYears and AddMonths methods.
(Note that these return new DateTime values - DateTime itself is
immutable.)

Weeks is just a matter of multiplying by 7 and treating as days.

Jon
 

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