2
Answers

How to Compare Two dates with Previous/Old Two dates

Rameez Javed

Rameez Javed

6y
519
1
I've to compare Start Date and End Date with the previous entry of Date duration in database, Actually i want to perform advance salary, I calculated No of Months between start date and end date and created the salary for calculated months in advance, now i want to make it sure that if i create the salary and months lies inside the advance salary created then show a validation error msg that Advance Salary Paid for this duration. 
 
Thanks in advance. 
Answers (2)
0
Rameez Javed

Rameez Javed

NA 395 64.8k 6y
Thanks Rabiranjan Singh
I don't think so it will compare with datadase previous entry of date duration, think about salary in advance and validation error on creation of same month salary that lies in previous advance salary... 
 
0
Rabiranjan Singh

Rabiranjan Singh

1.5k 199 780 6y
Hi Rameez Javed
You can compare two date simply as below if your date is   DateTime  type.
 
DateTime dt1 = DateTime.ParseExact("06-01-1995", "dd-MM-yyyy", null);
DateTime dt2 = DateTime.ParseExact("18-10-2018", "dd-MM-yyyy", null);
int cmp = dt1.CompareTo(dt2);
if (cmp > 0)
{
// date1 is greater means date1 is comes after date2
Console.WriteLine("date1 is greater means date1 is comes after date2");
}
else if (cmp < 0)
{
// date2 is greater means date1 is comes after date1
Console.WriteLine("date2 is greater means date1 is comes after date1");
}
else
{
// date1 is same as date2
Console.WriteLine("date1 is same as date2");
}
 
output 
date2 is greater means date1 is comes after date1 .
 
if you have still problem then feel free to comment