473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

add timespan to datetime

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

May 22 '07 #1
3 11255
Lars Schouw <sc******@yahoo.comwrote:
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;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 22 '07 #2
I just read the documentaion quickly does TimeSpan also work with
years, months, weeks?
I can find days.
Lars

On May 22, 3:28 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Lars Schouw <schou...@yahoo.comwrote:
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;

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 22 '07 #3
On May 22, 11:01 am, Lars Schouw <schou...@yahoo.comwrote:
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

May 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Justin Grieves | last post by:
Hello all, I've been trying to determine the difference in minutes between two DateTime objects (startTime and endTime) in C# using the TimeSpan object (ts). I am populating the two DateTime...
2
by: KK | last post by:
Hi I was testing a quicksort algorithm. long startTime = DateTime.Now.Ticks; //ticks before sorting begins q_sort( 0, mElements.Length-1 ); TimeSpan tp =new TimeSpan((DateTime.Now.Ticks -...
4
by: Jeroen CEuppens | last post by:
Hi, I want to measure a time (in ticks) between a couple of operations, with these code: long voor = DateTime.Now.TimeOfDay.Ticks; bmp1 = new Bitmap(@"\Network\im\ImageWeftIllum.bmp"); bmp2...
3
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
2
by: DWalker | last post by:
In Visual Studio (Visual Basic) .NET 2002, I noticed that this: Dim Elapsed as DateTime = Now - Now gives a "compile time" error (error in the IDE), saying that the '-' operator is not...
10
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
5
by: Vibhesh | last post by:
I am facing problem with TimeSpan structure when DirectX is used. Following is the sample code that causes the problem: ...
2
by: Bob | last post by:
Greetings All, Is there a simple way of suppressing the fractional part of a timespan At the moment I use one time span to populate another minus the fractional part .. this is a bit clunky. ...
2
by: shapper | last post by:
Hello, I have the following: boxStat = new BoxStat { BoxCount = database.Boxes.Count(), SinceLastCreate = (DateTime.Now - database.Boxes.Max(b => b.CreatedAt)).Days ?? 0 };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.