12

我正在尝试使用 wpf 制作日历。通过使用 itemsPanel 等,我有一个包含 7 列(周日至周六)和 6 行(每月的周数)的网格。如果我可以通过获取工作日和周数(每月)找到每个月第一天的起始位置,我怎样才能找到周数(每个月的 0-5)?我也不能以某种方式从那里填写日历吗?我迷路了,我不知道还能尝试什么。

public partial class SchedulePage : Page
{        
    MainWindow _parentForm;
    public int dayofweek;

    public SchedulePage(MainWindow parentForm)
    {
        InitializeComponent();
        _parentForm = parentForm;
        // DateTime date = new DateTime(year, month, day);

        _parentForm.bindings = new BindingCamper();          
        _parentForm.bindings.schedule.Add(new Schedule { WeekNo = (int) getWeekNumber(), WeekDay = dayofweek });
        DataContext = _parentForm.bindings;
        // lblTest.Content = dates(2011, 10, 27);
    }

    public double getWeekNumber()
    {
        dayofweek = getWeekDay(2011, 10, 31);
        double h = dayofweek / 7;
        double g = Math.Floor(h);
        return g;
    }

    public int getWeekDay(int year, int month, int day)
    {
        //year = 2011;
        //month = 10;
        //day = 27;
        int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
        // year -= month < 3;
        return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7;
    }
4

3 回答 3

17

您必须使用Calendar.GetDayOfWeekCalendar.GetWeekOfYear优先于自己编写。

您可以保证,如果您自己编写任何日期/时间处理代码,它将包含错误并且不会在不同的语言环境中工作。

public class Row
{
    public string MonthWeek { get; set; }
    public string Year { get; set; }
    public string Month { get; set; }
    public string Day { get; set; }
    public string WeekOfYear { get; set; }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var l = new List<Row>();
        DateTime startDate = DateTime.Now;
        DateTime d = new DateTime(startDate.Year, startDate.Month, 1);
        var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
        var ms = cal.GetWeekOfYear(new DateTime(d.Year, d.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
        for (var i = 1; d.Month == startDate.Month; d = d.AddDays(1))
        {
            var si = new Row();
            var month_week = (d.Day / 7) + 1;
            si.MonthWeek = month_week.ToString();
            si.Month = d.Year.ToString();
            si.Year = d.Month.ToString();
            si.Day = d.Day.ToString();
            si.WeekOfYear = cal.GetWeekOfYear(d, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
            l.Add(si);
        }
        dataGrid1.ItemsSource = l;
    }
}

与 XAML 中的强制性 DataGrid 一起:

    <DataGrid AutoGenerateColumns="true" Name="dataGrid1" />
于 2011-10-27T16:56:52.973 回答
11

您可以使用Calendar.GetWeekOfYearfrom Globalization 来执行此操作。

这是 MSDN 文档:http: //msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

CultureInfo.CurrentCulture您应该从to传递适当的区域性属性,GetWeekOfYear以便正确匹配当前区域性。

例子:

int GetWeekOfYear(DateTime date)
{
    return Calendar.GetWeekOfYear(
        date,
        CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
        CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
    );
}

您可以轻松地将其修改为以下扩展方法DateTime

static int GetWeekOfYear(this DateTime date)
{
    return Calendar.GetWeekOfYear(
        date,
        CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
        CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
    );
}
于 2011-10-27T15:49:32.410 回答
0

使用@Polynomial 答案,我有这个错误:

非静态字段、方法或属性需要对象引用...

如果您之前实例化了 GregorianCalendar,则可以调用 GetWeekOfYear 方法!

private static int GetWeekNumber(DateTime time)
{
    GregorianCalendar cal = new GregorianCalendar();
    int week = cal.GetWeekOfYear(time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
    return week;
}
于 2019-12-31T15:59:10.773 回答