1 Replies - 8192 Views - Last Post: 10 January 2008 - 03:42 PM Rate Topic: -----

#1 kayaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-October 07

datetime calculations

Post icon  Posted 10 January 2008 - 02:16 PM

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 ?

Is This A Good Question/Topic? 0
  • +

Replies To: datetime calculations

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4437
  • View blog
  • Posts: 12,308
  • Joined: 18-April 07

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.

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.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1