Results 1 to 4 of 4

Thread: [2.0] date math methods in c#..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    [2.0] date math methods in c#..

    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

  2. #2
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: [2.0] date math methods in c#..

    Quote Originally Posted by bnathvbdotnet
    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:

    Code:
    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
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] date math methods in c#..

    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.
    Code:
    DateTime today = DateTime.Today;
    DateTime date = new DateTime(1984, 10, 19);
    TimeSpan diff = today - date;
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: [2.0] date math methods in c#..

    Quote Originally Posted by jmcilhinney
    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

    Quote Originally Posted by jmcilhinney
    System.DateTime is a structure.
    Yeah, you are correct DateTime is a structure. Thanks for underlining it.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width