为什么我的C#程序不显示生日提醒?

时间:2016-05-21 15:22:58

标签: c# asp.net

我将此代码写入我的主类(C#)中以获取生日提醒。它曾经工作但现在却没有。我根本没有改变代码。问题可能是什么?

/// <summary>
/// Find all members that have birthdays occurring this week
/// </summary>
/// <returns>Returns an ArrayList containing members</returns>
public ArrayList BirthdaysThisWeek()
{
    ArrayList members = new ArrayList();

    DateTime today = DateTime.Today;

    //Work out how many days to add or to take away to get us to Monday
    int delta = DayOfWeek.Monday - today.DayOfWeek;
    //Change the date to point to Monday
    DateTime monday = today.AddDays(delta);
    //Make this date point to Sunday
    DateTime sunday = monday.AddDays(6);

    foreach (Member member in allMembers)
    {
        DateTime dob = member.DateOfBirth.Date;
        //Check to see if a member's birthdate is between Monday and Sunday
        if(dob >= monday.Date && dob <= sunday.Date)
        {
            //Add member to members ArrayList
            members.Add(member);
        }
    }

    //Return all members that have a birthday this week if none were     found return an empty ArrayList
    return members;
}

2 个答案:

答案 0 :(得分:1)

您的情况不起作用,因为您将生日的年,月,日与实际周的日期进行比较。在 CodeCaster 的评论之后我更改了我的答案,因为DayOfYear在2月28日闰年之后不能用于生日,你可以通过初始化dob DateTime来解决这个问题:

int actuelYear = member.DateOfBirth.Month == monday.Month ? monday.Year : sunday.Year;
DateTime dob = new DateTime(actuelYear, member.DateOfBirth.Month, member.DateOfBirth.Day);
if (dob.Date >= monday.Date && dob.Date <= sunday.Date)
{
    members.Add(member);
}

答案 1 :(得分:0)

除非他们本周出生,否则我不知道这是如何运作的。

你需要忽视他们出生的那一年。

public ArrayList BirthdaysThisWeek()
{
    ArrayList members = new ArrayList();

    DateTime today = DateTime.Today;
    int delta = DayOfWeek.Monday - today.DayOfWeek;
    DateTime monday = today.AddDays(delta);
    DateTime sunday = monday.AddDays(6);

    foreach (Member member in allMembers)
    {
        DateTime dob = member.DateOfBirth.Date;

        //Check to see if a member's birthdate is between Monday and Sunday
        if (IsBirthDayInRange(dob, monday, sunday)
        {
            //Add member to members ArrayList
            members.Add(member);
        }
    }
    return members;
}

参考:https://stackoverflow.com/a/2554881/495455

因此您需要将代码更改为:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = myTable.dequeueReusableCellWithIdentifier("editCell") as! EditTableViewCell

    cell.answerText.text = dictPicker[indexPath.row]![dictAnswer[indexPath.row]!]
    cell.questionView.text = listQuestion1[indexPath.row]
    cell.pickerDataSource = dictPicker[indexPath.row]!
    dictAnswer[indexPath.row] = cell.pickerValue
    cell.answerText.addTarget(self, action: #selector(AddFollowUpViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingDidEnd)
    cell.answerText.tag = indexPath.row
    cell.identifier = true

    return cell
}