开发者

How to compate two date values?

I have one entry form in which user fills the form with the cur开发者_运维技巧rent date everyday. Now, I need to display the dates which the user not entered in the form.

I have retrieved entered dates and number of days in the month. I don't know how to compare this both values and print the dates which not entered.

Thanks in advance.


Basically, you will want to start on the first of the month, and create a DateTime value for each of the days in the month. Compare each of these days to all entered values for the month, and if none match, output the generated DateTime. Here's a basic algorithm to start you off; you can modify it as necessary to cover the span of dates you need:

//I leave you the exercise of actually getting the entered dates in DateTime format
List<DateTime> enteredDates = GetEnteredDatesAsDateTimes();
var unenteredDates = new List<DateTime>();

//I assume you want days for the current month; 
//if not you can set up a DateTime using a specified month/year
var today = DateTime.Today;
var dateToCheck = new DateTime(today.Year, today.Month, 1);

//You could also make sure the date is less than the current date,
//or less than a specified "end date".
while (dateToCheck.Month == today.Month)
{
   //uses Linq, which requires .NET 3.5
   if(!enteredDates.Any(d=>d.Date == dateToCheck.Date))
      unenteredDates.Add(dateToCheck);

   //use the below code instead for .NET 2.0
   //bool inList = false;
   //foreach(var date in enteredDates)
   //   if(enteredDate.Date == date.Date)
   //   {
   //      inList = true;
   //      break;
   //   }
   //if(!inList) unenteredDates.Add(dateToCheck);

   dateToCheck = dateToCheck.AddDays(1);
}

//unenteredDates now has all the dates for which the user didn't fill out the form.

Understand that this is an N^2-complexity algorithm; you're comparing each element of one list to each element of another, expecting the two lists to be of roughly equal cardinality. It shouldn't perform terribly, as long as you're not checking several months' worth of dates. You can reduce this to NlogN by sorting the list of entered dates, then performing a binary search for the date instead of a linear one. That increases the code you need to write but reduces the steps the code needs to take.


Provided you have the ability to keep track of which dates the form was filled out. All you have to do write a loop with the length of the current month.

At that point all you would have to do is a simple check to see if the current iteration exists in your collection of dates. If the date is not contained within the collection print the result.

System.DateTime.DaysInMonth(int year, int month) will determine how many iterations of the loop there should be.

All you have to do is extract the current: Month and Year.


The simplest way I can think of is to print all the dates of the year, skipping any dates that have been entered in the form. for example:

for(int i=1;i<13;i++){
    String month = Integer.toString(i);
    for(int j=1;j<32;j++){
        String date = month+"/"+Integer.toString(j); 
        if (date in listOfValidDates):
            if(date in filedDates):
                continue;
            System.out.println(date)
        }
     }

update: changed the example from pseudocode to java, because that's the language I know that's most similar to C#.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜