Click to See Complete Forum and Search --> : [2.0] date math methods in c#..
bnathvbdotnet
Nov 24th, 2006, 01:45 PM
In my application, I have lot of date math that should be done.
which is the best way to go about date math. I heard date math in c# is pretty complicated. some one suggested to import vb.net's date name space and use them in stead of c#. If that is a best route to go about for date math in c#, how can I import vb.net's system.date name space?
any suggestion?
thanks
nath
BramVandenbon
Nov 24th, 2006, 04:04 PM
In my application, I have lot of date math that should be done.
which is the best way to go about date math. I heard date math in c# is pretty complicated. some one suggested to import vb.net's date name space and use them in stead of c#. If that is a best route to go about for date math in c#, how can I import vb.net's system.date name space?
any suggestion?
thanks
nath
I don't know anything about System.Date. It's the first time I hear anything about that. I didn't know VB.NET has a difference class for this.
If I were you I would just use the DateTime class. It's not that complicated at all in my oppinion.
I will give some examples of what you can do:
DateTime today = DateTime.Now; //the current day and time
DateTime date = new DateTime(1984, 10, 19); //19 october 1984
DateTime spendtime = Datetime.Substract(date);
if (today.DayOfWeek == DayOfWeek.Monday){
Console.WriteLine("It's monday today !!");
}
Couldn't be any easier could it?
Besides that there are is also the TimeSpan class for calculations with time. And each of these classes has a lot of specialized ToString() methods for special kind of notations of the date.
I hope this is a good answer :)
jmcilhinney
Nov 24th, 2006, 06:26 PM
There is no such namespace as System.Date and DateTime is not a class. In VB.NET you can use members of the Microsoft.VisualBasic.DateAndTime module, and System.DateTime is a structure.
If you want to make use of VB Runtime functions then you need to reference the Microsoft.VisualBasic.dll assembly in your C# project, just like any other .NET assembly. Its member modules are then treated as static classes.
Having said that, this would not be considered a good idea by many people. First of all, the main attraction of DateAndTime is the DateDiff function, but that's not 100% accurate anyway.
BV is quite correct that date arithmetic is quite simple. There are a couple of issues with that code though. For a start, when you subtract one DateTime from another you get a TimeSpan object, not another DateTime. Note also that you can use standard arithmetic operators with DateTime objects. I'm not sure that you could in .NET 1.x, where methods like Subtract were required, but you certainly can in 2.0, e.g.DateTime today = DateTime.Today;
DateTime date = new DateTime(1984, 10, 19);
TimeSpan diff = today - date;
BramVandenbon
Nov 24th, 2006, 07:39 PM
BV is quite correct that date arithmetic is quite simple. There are a couple of issues with that code though. For a start, when you subtract one DateTime from another you get a TimeSpan object, not another DateTime.
Yeah, I was confusing with the other overload. The Substract has 2 overloads:
DateTime Substract (TimeSpan);
TimeSpan Substract (DateTime);
That's what I get when I post without testing it out first :afrog:
System.DateTime is a structure.
Yeah, you are correct DateTime is a structure. Thanks for underlining it. :duck:
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.