如何确定特定日期的周数?

发布于 2024-12-12 17:23:44 字数 1235 浏览 2 评论 0原文

我正在尝试使用 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;
    }

I'm trying to make a calendar using wpf. By using itemsPanel and more, I have a grid with 7 columns(sunday-saturday) and 6 rows(week# of month). If i can find the starting position of the first of each month by getting the weekday and week number(of the month), how can I find the week number(0-5 of each month)? Also can't I somehow just fill in the calendar from there? I'm lost and I don't know what else to try.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林散布 2024-12-19 17:23:44

您必须使用 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" />

You must use Calendar.GetDayOfWeek and Calendar.GetWeekOfYear in preference to writing yourself.

You can guarantee that if you write any date / time handling code yourself it will contain faults and won't work in different locales.

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

together with the obligatory DataGrid in the XAML:

    <DataGrid AutoGenerateColumns="true" Name="dataGrid1" />
苏佲洛 2024-12-19 17:23:44

您可以使用 Globalization 中的 Calendar.GetWeekOfYear 来执行此操作。

以下是它的 MSDN 文档:http://msdn。 microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

您应该从以下位置传递适当的区域性属性将 CultureInfo.CurrentCulture 更改为 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
    );
}

You can use Calendar.GetWeekOfYear from Globalization to do this.

Here's the MSDN docs for it: http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

You should pass the appropriate culture properties from CultureInfo.CurrentCulture to GetWeekOfYear so that you match the current culture properly.

Example:

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

You could easily modify this into an extension method on DateTime:

static int GetWeekOfYear(this DateTime date)
{
    return Calendar.GetWeekOfYear(
        date,
        CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
        CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
    );
}
眼趣 2024-12-19 17:23:44

使用@Polynomial 答案,我遇到此错误:

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

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

private static int GetWeekNumber(DateTime time)
{
    GregorianCalendar cal = new GregorianCalendar();
    int week = cal.GetWeekOfYear(time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
    return week;
}

With @Polynomial answer, I have this error:

An object reference is required for the non-static field, method, or property...

If you instanciate GregorianCalendar before then you can call the method GetWeekOfYear !

private static int GetWeekNumber(DateTime time)
{
    GregorianCalendar cal = new GregorianCalendar();
    int week = cal.GetWeekOfYear(time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
    return week;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文