i am trying to find a method in C# equivalent to java.util.Calendar.add(int field, int amount) but all i am finding is literal methods in System.Globalization.Calendar, i.e. AddYears, AddMonths etc.
this literal anti-minimization is seriously irking my minimalist instinct as i like to write elegant code without spaghetti-looking IF or SWITCH statements to determine which method to use based on the client-provided argument.
can anybody throw their $0.02 as to where, in C#, i can find what i am looking for ?
datetime calculations
Page 1 of 11 Replies - 7572 Views - Last Post: 10 January 2008 - 03:42 PM
Replies To: datetime calculations
#2
Re: datetime calculations
Posted 10 January 2008 - 03:42 PM
Well first all the .NET languages are a bit more verbose than Java so the minimalistic thing will have to get better if you wish to use .NET. But most anything time related can be found in the DateTime object. Here you can add the classic days, months, years, milliseconds, UTC, time converting etc.
Now keep in mind that you can do things like specify adding days and add 365 to make a year etc. You can use negative numbers etc. This will give you a bit more flexibility with dates and time than provided by the System.Globalization.Calendar object.
The one that probably most closely matches is the DateTime method "Add" which takes a timespan object. The timespan object you can setup with a timespan to add to the date specified by the datetime.
Hopefully this is what you were looking for when dealing with time variables. Of course once you do this you can set calendars to read this datetime object etc.
Now keep in mind that you can do things like specify adding days and add 365 to make a year etc. You can use negative numbers etc. This will give you a bit more flexibility with dates and time than provided by the System.Globalization.Calendar object.
The one that probably most closely matches is the DateTime method "Add" which takes a timespan object. The timespan object you can setup with a timespan to add to the date specified by the datetime.
DateTime dtNow = DateTime.Now; // Add four hours to the current time and show it in a message box. dtNow =dtNow.Add(TimeSpan.FromHours(4.0)); MessageBox.Show(dtNow.ToString());
Hopefully this is what you were looking for when dealing with time variables. Of course once you do this you can set calendars to read this datetime object etc.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|