Click here to Skip to main content
15,886,080 members
Home / Discussions / C#
   

C#

 
GeneralRe: problem when launching application Pin
Ibrahim.elh23-Oct-14 23:15
Ibrahim.elh23-Oct-14 23:15 
AnswerRe: problem when launching application Pin
eljainc24-Oct-14 10:59
eljainc24-Oct-14 10:59 
QuestionGenerate xls file Windows Service Pin
Member 841450123-Oct-14 11:05
Member 841450123-Oct-14 11:05 
AnswerRe: Generate xls file Windows Service Pin
Eddy Vluggen24-Oct-14 1:49
professionalEddy Vluggen24-Oct-14 1:49 
QuestionHow to retain the drop down (which is loaded from database) values when page posted to server in MVC (c#)..? Pin
Kranthikumar.polasa23-Oct-14 8:06
Kranthikumar.polasa23-Oct-14 8:06 
QuestionSend SMS Messages Pin
Kevin Marois23-Oct-14 4:56
professionalKevin Marois23-Oct-14 4:56 
AnswerRe: Send SMS Messages Pin
BillWoodruff23-Oct-14 6:51
professionalBillWoodruff23-Oct-14 6:51 
QuestionHow do I use this class Pin
MarkB12323-Oct-14 2:12
MarkB12323-Oct-14 2:12 
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)
{
if (dt2.Second < dt1.Second)
{
dd._Years--;
}
}
}
}
}
}

dd._Months = dt2.Month - dt1.Month;
if (dt2.Month < dt1.Month)
{
dd._Months = 12 - dt1.Month + dt2.Month;
}
if (dd._Months > 0)
{
if (dt2.Day < dt1.Day)
{
dd._Months--;
}
else if (dt2.Day == dt1.Day)
{
if (dt2.Hour < dt1.Hour)
{
dd._Months--;
}
else if (dt2.Hour == dt1.Hour)
{
if (dt2.Minute < dt1.Minute)
{
dd._Months--;
}
else if (dt2.Minute == dt1.Minute)
{
if (dt2.Second < dt1.Second)
{
dd._Months--;
}
}
}
}
}

dd._Days = dt2.Day - dt1.Day;
if (dt2.Day < dt1.Day)
{
dd._Days = DateTime.DaysInMonth(dt1.Year, dt1.Month) - dt1.Day + dt2.Day;
}
if (dd._Days > 0)
{
if (dt2.Hour < dt1.Hour)
{
dd._Days--;
}
else if (dt2.Hour == dt1.Hour)
{
if (dt2.Minute < dt1.Minute)
{
dd._Days--;
}
else if (dt2.Minute == dt1.Minute)
{
if (dt2.Second < dt1.Second)
{
dd._Days--;
}
}
}
}

dd._Weeks = dd._Days / 7;
dd._Days = dd._Days % 7;

dd._Hours = dt2.Hour - dt1.Hour;
if (dt2.Hour < dt1.Hour)
{
dd._Hours = 24 - dt1.Hour + dt2.Hour;
}
if (dd._Hours > 0)
{
if (dt2.Minute < dt1.Minute)
{
dd._Hours--;
}
else if (dt2.Minute == dt1.Minute)
{
if (dt2.Second < dt1.Second)
{
dd._Hours--;
}
}
}

dd._Minutes = dt2.Minute - dt1.Minute;
if (dt2.Minute < dt1.Minute)
{
dd._Minutes = 60 - dt1.Minute + dt2.Minute;
}
if (dd._Minutes > 0)
{
if (dt2.Second < dt1.Second)
{
dd._Minutes--;
}
}

dd._Seconds = dt2.Second - dt1.Second;
if (dt2.Second < dt1.Second)
{
dd._Seconds = 60 - dt1.Second + dt2.Second;
}
return dd;
}
}
AnswerRe: How do I use this class Pin
Richard MacCutchan23-Oct-14 2:35
mveRichard MacCutchan23-Oct-14 2:35 
AnswerRe: How do I use this class Pin
OriginalGriff23-Oct-14 2:38
mveOriginalGriff23-Oct-14 2:38 
GeneralRe: How do I use this class Pin
MarkB12323-Oct-14 2:57
MarkB12323-Oct-14 2:57 
GeneralRe: How do I use this class Pin
OriginalGriff23-Oct-14 3:02
mveOriginalGriff23-Oct-14 3:02 
GeneralRe: How do I use this class Pin
MarkB12323-Oct-14 3:12
MarkB12323-Oct-14 3:12 
GeneralRe: How do I use this class Pin
Richard MacCutchan23-Oct-14 5:23
mveRichard MacCutchan23-Oct-14 5:23 
AnswerRe: How do I use this class Pin
BillWoodruff23-Oct-14 3:34
professionalBillWoodruff23-Oct-14 3:34 
AnswerRe: How do I use this class Pin
jschell23-Oct-14 8:45
jschell23-Oct-14 8:45 
QuestionTeamviewer Protocol Pin
Jassim Rahma22-Oct-14 23:11
Jassim Rahma22-Oct-14 23:11 
AnswerRe: Teamviewer Protocol Pin
Pete O'Hanlon22-Oct-14 23:48
mvePete O'Hanlon22-Oct-14 23:48 
AnswerRe: Teamviewer Protocol Pin
Richard MacCutchan23-Oct-14 0:31
mveRichard MacCutchan23-Oct-14 0:31 
GeneralRe: Teamviewer Protocol Pin
Jassim Rahma23-Oct-14 1:26
Jassim Rahma23-Oct-14 1:26 
GeneralRe: Teamviewer Protocol Pin
Richard MacCutchan23-Oct-14 2:30
mveRichard MacCutchan23-Oct-14 2:30 
AnswerRe: Teamviewer Protocol Pin
Thomas Mahlberg23-Oct-14 6:13
professionalThomas Mahlberg23-Oct-14 6:13 
Question"This command is not available because no document is open." Pin
Tejas Shastri22-Oct-14 12:13
Tejas Shastri22-Oct-14 12:13 
AnswerRe: "This command is not available because no document is open." Pin
Richard Andrew x6422-Oct-14 13:04
professionalRichard Andrew x6422-Oct-14 13:04 
GeneralRe: "This command is not available because no document is open." Pin
Tejas Shastri23-Oct-14 0:45
Tejas Shastri23-Oct-14 0:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.