简体   繁体   中英

How to Compare Two dates with Previous/Old Two dates

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.

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}");

            }
        }
    }
}

将您过去和当前日期转换为字符串,然后进行比较,因为日期/时间/年/月,即日期始终为.date格式,因此您必须在字符串中进行更改,然后进行比较

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM