How do I use this class
-
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)
-
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)
-
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)
-
-
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...
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...
-
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 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)
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
-
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]). :)
-
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)
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.