0

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 message that Advance Salary Paid for this duration.

Thanks in advance.

Rameez Javed
  • 139
  • 3
  • 9

2 Answers2

0

I think the below should work. Not 100% sure if it's the most efficient or follows the writing conventions as I'm still learning C#. If so please point out where I can improve. But basically the logic gets the old start and end date works out the difference in days and adds it to the start and compares to see if the startDate is greater than the new date. Then the difference is added if so throws an validation message. You'll have to implement this in your own way as you supplied no code. (You need to do the same for end I just forgot to).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackQuesionsPractice
{
    class Program
    {
        static void Main(string[] args)
        {

            //These will be the current dates in your database which you said you use to calculate the months * salary
            DateTime CurrentStart = new DateTime(2013, 1, 1);
            DateTime CurrentEnd = new DateTime(2014, 2, 1);

            //Work out the how the difference in months between the start and end date
            var diffMonths = (CurrentEnd.Month + CurrentEnd.Year * 12) - (CurrentStart.Month + CurrentStart.Year * 12);

            //Monthly salary * by the difference in months to work out the expected wage due
            int MonthlySalary = 40 * diffMonths;

            //User inserts the new dates into the database
            DateTime NewStart = new DateTime(2013, 5, 1);
            DateTime NewEnd = new DateTime(2015, 2, 1);

            //Workout out the total days between the old start and end date
            var diffDays = (CurrentEnd - CurrentStart).TotalDays;

            //Add the total days onto the old date and check to see it it's greater than the current date
            if (CurrentStart.AddDays(diffDays) > NewStart)
            {
                Console.WriteLine("The newly entered start date falls within the last date range saved in the database");
            }
            else {

                //Salary due to the employee 
                Console.WriteLine($"The amount of salary due is: {MonthlySalary}");

            }
        }
    }
}
RumbleJungle
  • 100
  • 6
  • sir you are trying to pay the daily wage, and your calculation is based on Per Day Wage * Total Days in StartDate and EndDate, i want to get it like, If i paid advance salary to an employee for Sep 2018 to Jan 2019 duration, and In Jan 2019 admin is creating salary for Salary month Jan 2019, In the mean while i want to check that this employee has already paid advance salary for 5 months if the Jan 2019 included in previous salary duration Sep1,2018 to Jan31 2019, how can i check the date duration of Jan 2019 lies in previous Advance Salary duration??? – Rameez Javed Oct 22 '18 at 12:46
  • The calculation is based on monthly wage. int MonthlySalary = 40 * diffMonths; the above assumes the salary is 40 per month * the period between start and end date. The above should work as you want as it checks the new start date isn't within the previously entered period. If you change DateTime NewStart = new DateTime(2014, 1, 1); to (2014, 2, 1) it shows the employee should be paid. You'll need to add an if statement to workout how much as I've only done validation of the old date range. – RumbleJungle Oct 22 '18 at 13:24
  • Thank you, RumbleJungle, I created a test project to cheeck it, here is something unclear that the diff of month you told is excluding one month, if i'm creating salary for Sep2018 to April2019 in Advance then month difference should be 8, including the Sep and April, but it show 7. Anyhow Thanks, i'm testing it will ping you if got any issue regarding this... – Rameez Javed Oct 23 '18 at 05:14
-2

convert your past and current date into strings and then compare because date/time/year/month i.e. Date always in .date format so you have to change it in the String and then compare it

  • 2
    Why would have to you convert DateTime objects to strings in order to compare them? – CodeCaster Oct 22 '18 at 10:47
  • if you don't want to convert to String than it uses the Date property on each of them (startDate and EndDate). if (a.Date >= b.Date) – Sohrab Ansari Oct 22 '18 at 10:53
  • I'm asking why you would suggest doing something like that. – CodeCaster Oct 22 '18 at 10:54
  • because sometimes user uses current date (System Date) that is in .date format and maybe his past date is saved in string format may be that's conflict so i have suggested the conversion! – Sohrab Ansari Oct 22 '18 at 10:57
  • 1
    Yeah sorry but that makes no sense whatsoever. You can just compare DateTimes without being bothered about its format. It's when converting to string that a format comes into play. – CodeCaster Oct 22 '18 at 10:58
  • thanks for the information #CodeCaster what I have suggested that all from my information – Sohrab Ansari Oct 22 '18 at 11:00