【问题标题】:Get dates by month in C# Windows Phone 7在 C# Windows Phone 7 中按月获取日期
【发布时间】:2013-11-28 13:15:24
【问题描述】:

以下代码在我的 windows phone 7 应用程序中显示为特定日期安排的约会列表

private void DisplayAppointmentsForDate(DateTime date)
{

    appointmentsSource.FetchData(date, date.AddDays(1));
    this.AppointmentsList.ItemsSource = appointmentsSource.GetAppointments((IAppointment appointment) =>
    {
        DateTime currentAppointmentStart = appointment.StartDate;
        DateTime currentAppointmentEnd = appointment.EndDate;
        DateTime requiredAppointmentsStartDate = date.Date;
        DateTime requiredAppointmentsEndDate = date.Date.AddDays(1);


        if (requiredAppointmentsEndDate > currentAppointmentStart && requiredAppointmentsStartDate < currentAppointmentEnd)
        {
            return true;
        }

        return false;
    });
}

private void RadCalendar_SelectedValueChanged(object sender, ValueChangedEventArgs<object> e)
{
    if (e.NewValue == null)
    {
        this.AppointmentsList.ItemsSource = null;
        return;
    }
    DisplayAppointmentsForDate((e.NewValue as DateTime?).Value);
}

但这显示了为特定日期安排的约会。例如。如果在日历上选择了 23/09/2013,则列表将显示当天的所有约会。

我想更改此设置,以便列表显示特定月份的所有约会。例如。如果日历是 2013 年 7 月,则列表应显示该月的所有约会。

开始和结束日期都设置为 DateTime 类型,其值例如 23/09/2013 12:01PM,这就是它用于获取应用列表的方式。

我尝试过使用appointmentsSource.FetchData(date.Month, date.AddDays(1));,但它不起作用。

我该怎么做?

【问题讨论】:

  • 你已经尝试过什么?特别是,您的FetchData 电话看起来就像您需要它做的一切,只要您给出一个月的开始和下个月的开始......

标签: c# xaml windows-phone-7 datetime visual-studio-2012


【解决方案1】:

我假设 FetchData 方法接受一个“开始日期”参数和一个“结束日期参数”,在这种情况下,您只需要获取当月的第一个日期和当月的最后一个日期

        var currentDate = DateTime.Now; // Or whatever date you want to displa
        var startDate = new DateTime(currentDate.Year, currentDate.Month, 1);
        var endDate = new DateTime(currentDate.Year, currentDate.Month, DateTime.DaysInMonth(currentDate.Year, currentDate.Month));

        appointmentsSource.FetchData(startDate, endDate);

如果您的 FetchData 采用月份参数和天数,则

 appointmentsSource.FetchData(currentDate.Month, DateTime.DaysInMonth(currentDate.Year, currentDate.Month));

我们确实需要看到“FetchData”方法才能为您提供明确的答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 2011-11-17
    相关资源
    最近更新 更多