Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I use this class

How do I use this class

Scheduled Pinned Locked Moved C#
questionhelp
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarkB123
    wrote on last edited by
    #1

    I found this class on a forum... How do I use it - I can't seem to callthe Difference method in bold text? I'm currently trying... DateDiff dateDiff = new DateDiff(); dateDiff.Difference((DateTime)StartDate, (DateTime)EndDate); // Doesn't Work Many thanks for any help. public class DateDiff { private int _Years = 0; private int _Months = 0; private int _Weeks = 0; private int _Days = 0; private int _Hours = 0; private int _Minutes = 0; private int _Seconds = 0; public int Years { get { return _Years; } } public int Months { get { return _Months; } } public int Weeks { get { return _Weeks; } } public int Days { get { return _Days; } } public int Hours { get { return _Hours; } } public int Minutes { get { return _Minutes; } } public int Seconds { get { return _Seconds; } } public override string ToString() { return String.Format("{0} year{1}, {2} month{3}, {4} week{5}, {6} day{7}, {8} hour{9}, {10} minute{11}, {12} second{13}" , Years, Years != 1 ? "s" : "" , Months, Months != 1 ? "s" : "" , Weeks, Weeks != 1 ? "s" : "" , Days, Days != 1 ? "s" : "" , Hours, Hours != 1 ? "s" : "" , Minutes, Minutes != 1 ? "s" : "" , Seconds, Seconds != 1 ? "s" : "" ); } static public DateDiff Difference(DateTime dt1, DateTime dt2) { if (dt1 > dt2) { DateTime dtTemp = dt1; dt1 = dt2; dt2 = dtTemp; } DateDiff dd = new DateDiff(); dd._Years = dt2.Year - dt1.Year; if (dd._Years > 0) { if (dt2.Month < dt1.Month) { dd._Years--; } else if (dt2.Month == dt1.Month) { if (dt2.Day < dt1.Day) { dd._Years--; } else if (dt2.Day == dt1.Day) { if (dt2.Hour < dt1.Hour) { dd._Years--; } else if (dt2.Hour == dt1.Hour) { if (dt2.Minute < dt1.Minute) { dd._Years--; } else if (dt2.Minute == dt1.Minute)

    L OriginalGriffO B J 4 Replies Last reply
    0
    • M MarkB123

      I found this class on a forum... How do I use it - I can't seem to callthe Difference method in bold text? I'm currently trying... DateDiff dateDiff = new DateDiff(); dateDiff.Difference((DateTime)StartDate, (DateTime)EndDate); // Doesn't Work Many thanks for any help. public class DateDiff { private int _Years = 0; private int _Months = 0; private int _Weeks = 0; private int _Days = 0; private int _Hours = 0; private int _Minutes = 0; private int _Seconds = 0; public int Years { get { return _Years; } } public int Months { get { return _Months; } } public int Weeks { get { return _Weeks; } } public int Days { get { return _Days; } } public int Hours { get { return _Hours; } } public int Minutes { get { return _Minutes; } } public int Seconds { get { return _Seconds; } } public override string ToString() { return String.Format("{0} year{1}, {2} month{3}, {4} week{5}, {6} day{7}, {8} hour{9}, {10} minute{11}, {12} second{13}" , Years, Years != 1 ? "s" : "" , Months, Months != 1 ? "s" : "" , Weeks, Weeks != 1 ? "s" : "" , Days, Days != 1 ? "s" : "" , Hours, Hours != 1 ? "s" : "" , Minutes, Minutes != 1 ? "s" : "" , Seconds, Seconds != 1 ? "s" : "" ); } static public DateDiff Difference(DateTime dt1, DateTime dt2) { if (dt1 > dt2) { DateTime dtTemp = dt1; dt1 = dt2; dt2 = dtTemp; } DateDiff dd = new DateDiff(); dd._Years = dt2.Year - dt1.Year; if (dd._Years > 0) { if (dt2.Month < dt1.Month) { dd._Years--; } else if (dt2.Month == dt1.Month) { if (dt2.Day < dt1.Day) { dd._Years--; } else if (dt2.Day == dt1.Day) { if (dt2.Hour < dt1.Hour) { dd._Years--; } else if (dt2.Hour == dt1.Hour) { if (dt2.Minute < dt1.Minute) { dd._Years--; } else if (dt2.Minute == dt1.Minute)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Why not use the .NET DateTime Structure[^] which offers such features, and more? And. more importantly, it is fully documented and supported.

      1 Reply Last reply
      0
      • M MarkB123

        I found this class on a forum... How do I use it - I can't seem to callthe Difference method in bold text? I'm currently trying... DateDiff dateDiff = new DateDiff(); dateDiff.Difference((DateTime)StartDate, (DateTime)EndDate); // Doesn't Work Many thanks for any help. public class DateDiff { private int _Years = 0; private int _Months = 0; private int _Weeks = 0; private int _Days = 0; private int _Hours = 0; private int _Minutes = 0; private int _Seconds = 0; public int Years { get { return _Years; } } public int Months { get { return _Months; } } public int Weeks { get { return _Weeks; } } public int Days { get { return _Days; } } public int Hours { get { return _Hours; } } public int Minutes { get { return _Minutes; } } public int Seconds { get { return _Seconds; } } public override string ToString() { return String.Format("{0} year{1}, {2} month{3}, {4} week{5}, {6} day{7}, {8} hour{9}, {10} minute{11}, {12} second{13}" , Years, Years != 1 ? "s" : "" , Months, Months != 1 ? "s" : "" , Weeks, Weeks != 1 ? "s" : "" , Days, Days != 1 ? "s" : "" , Hours, Hours != 1 ? "s" : "" , Minutes, Minutes != 1 ? "s" : "" , Seconds, Seconds != 1 ? "s" : "" ); } static public DateDiff Difference(DateTime dt1, DateTime dt2) { if (dt1 > dt2) { DateTime dtTemp = dt1; dt1 = dt2; dt2 = dtTemp; } DateDiff dd = new DateDiff(); dd._Years = dt2.Year - dt1.Year; if (dd._Years > 0) { if (dt2.Month < dt1.Month) { dd._Years--; } else if (dt2.Month == dt1.Month) { if (dt2.Day < dt1.Day) { dd._Years--; } else if (dt2.Day == dt1.Day) { if (dt2.Hour < dt1.Hour) { dd._Years--; } else if (dt2.Hour == dt1.Hour) { if (dt2.Minute < dt1.Minute) { dd._Years--; } else if (dt2.Minute == dt1.Minute)

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Try:

        DateDiff diff = DateDiff.Difference(dt1, dt2);

        But...You do realize there is a TimeSpan structure already in the .NET framework? MSDN[^] So you could just say:

        TimeSpan diff = dt2 - dt1;

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        M 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Try:

          DateDiff diff = DateDiff.Difference(dt1, dt2);

          But...You do realize there is a TimeSpan structure already in the .NET framework? MSDN[^] So you could just say:

          TimeSpan diff = dt2 - dt1;

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          M Offline
          M Offline
          MarkB123
          wrote on last edited by
          #4

          Thanks for replying. I realize there is a TimeSpan but it's no use to me as it doesn't show Weeks or Years elapsed and I need them. Your example worked (many thanks) - I hadn't noticed that the method was static...

          OriginalGriffO 1 Reply Last reply
          0
          • M MarkB123

            Thanks for replying. I realize there is a TimeSpan but it's no use to me as it doesn't show Weeks or Years elapsed and I need them. Your example worked (many thanks) - I hadn't noticed that the method was static...

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Have a look at this: Working with Age: it's not the same as a TimeSpan![^]

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            M 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Have a look at this: Working with Age: it's not the same as a TimeSpan![^]

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              M Offline
              M Offline
              MarkB123
              wrote on last edited by
              #6

              Many thanks for the link - It doesn't quite go far enough for me though as I also need access to the hours and seconds (which the class I posted gives me - [I need to test it though]). :)

              L 1 Reply Last reply
              0
              • M MarkB123

                I found this class on a forum... How do I use it - I can't seem to callthe Difference method in bold text? I'm currently trying... DateDiff dateDiff = new DateDiff(); dateDiff.Difference((DateTime)StartDate, (DateTime)EndDate); // Doesn't Work Many thanks for any help. public class DateDiff { private int _Years = 0; private int _Months = 0; private int _Weeks = 0; private int _Days = 0; private int _Hours = 0; private int _Minutes = 0; private int _Seconds = 0; public int Years { get { return _Years; } } public int Months { get { return _Months; } } public int Weeks { get { return _Weeks; } } public int Days { get { return _Days; } } public int Hours { get { return _Hours; } } public int Minutes { get { return _Minutes; } } public int Seconds { get { return _Seconds; } } public override string ToString() { return String.Format("{0} year{1}, {2} month{3}, {4} week{5}, {6} day{7}, {8} hour{9}, {10} minute{11}, {12} second{13}" , Years, Years != 1 ? "s" : "" , Months, Months != 1 ? "s" : "" , Weeks, Weeks != 1 ? "s" : "" , Days, Days != 1 ? "s" : "" , Hours, Hours != 1 ? "s" : "" , Minutes, Minutes != 1 ? "s" : "" , Seconds, Seconds != 1 ? "s" : "" ); } static public DateDiff Difference(DateTime dt1, DateTime dt2) { if (dt1 > dt2) { DateTime dtTemp = dt1; dt1 = dt2; dt2 = dtTemp; } DateDiff dd = new DateDiff(); dd._Years = dt2.Year - dt1.Year; if (dd._Years > 0) { if (dt2.Month < dt1.Month) { dd._Years--; } else if (dt2.Month == dt1.Month) { if (dt2.Day < dt1.Day) { dd._Years--; } else if (dt2.Day == dt1.Day) { if (dt2.Hour < dt1.Hour) { dd._Years--; } else if (dt2.Hour == dt1.Hour) { if (dt2.Minute < dt1.Minute) { dd._Years--; } else if (dt2.Minute == dt1.Minute)

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                imho, the code you found is very poorly written, and to attempt to modify it is a waste of time. CodeProject has a C# library for every possible DateTime calculation of exceptional quality by Jani Giannoudis, "Time Period Library:" [^].

                « There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali

                1 Reply Last reply
                0
                • M MarkB123

                  Many thanks for the link - It doesn't quite go far enough for me though as I also need access to the hours and seconds (which the class I posted gives me - [I need to test it though]). :)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  DateTime and TimeSpan give you everything you want, as I said in my earlier post. But you do have to read the documentation.

                  1 Reply Last reply
                  0
                  • M MarkB123

                    I found this class on a forum... How do I use it - I can't seem to callthe Difference method in bold text? I'm currently trying... DateDiff dateDiff = new DateDiff(); dateDiff.Difference((DateTime)StartDate, (DateTime)EndDate); // Doesn't Work Many thanks for any help. public class DateDiff { private int _Years = 0; private int _Months = 0; private int _Weeks = 0; private int _Days = 0; private int _Hours = 0; private int _Minutes = 0; private int _Seconds = 0; public int Years { get { return _Years; } } public int Months { get { return _Months; } } public int Weeks { get { return _Weeks; } } public int Days { get { return _Days; } } public int Hours { get { return _Hours; } } public int Minutes { get { return _Minutes; } } public int Seconds { get { return _Seconds; } } public override string ToString() { return String.Format("{0} year{1}, {2} month{3}, {4} week{5}, {6} day{7}, {8} hour{9}, {10} minute{11}, {12} second{13}" , Years, Years != 1 ? "s" : "" , Months, Months != 1 ? "s" : "" , Weeks, Weeks != 1 ? "s" : "" , Days, Days != 1 ? "s" : "" , Hours, Hours != 1 ? "s" : "" , Minutes, Minutes != 1 ? "s" : "" , Seconds, Seconds != 1 ? "s" : "" ); } static public DateDiff Difference(DateTime dt1, DateTime dt2) { if (dt1 > dt2) { DateTime dtTemp = dt1; dt1 = dt2; dt2 = dtTemp; } DateDiff dd = new DateDiff(); dd._Years = dt2.Year - dt1.Year; if (dd._Years > 0) { if (dt2.Month < dt1.Month) { dd._Years--; } else if (dt2.Month == dt1.Month) { if (dt2.Day < dt1.Day) { dd._Years--; } else if (dt2.Day == dt1.Day) { if (dt2.Hour < dt1.Hour) { dd._Years--; } else if (dt2.Hour == dt1.Hour) { if (dt2.Minute < dt1.Minute) { dd._Years--; } else if (dt2.Minute == dt1.Minute)

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    You might want to verify your requirements first before attempting something like this. For example how many "days" exist between the following periods? How many "hours"?How many "days"? And does the answer change if the time zone does? - July 7, 23:40 to July 8, 00:15 - Dec 31, 23:40 to Jan 1, 00:15 These are often business requirements and depends on what business needs are. As another extreme example if one needs seven "business" days around December 25th then what impacts do holidays and weekends have on what the actual span is.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups