如何确定某个日期的周数? [英] How can I determine the week number of a certain date?

查看:118
本文介绍了如何确定某个日期的周数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用wpf制作日历。通过使用itemsPanel和更多,我有一个网格有7列(星期日 - 星期六)和6行(每月的周数)。如果我可以通过获取工作日和周数(月份)找到每个月第一个月的起始位置,我如何找到周数(每月0-5个)?也不能以某种方式从那里填写日历?我失去了,我不知道还有什么尝试。

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


推荐答案

您必须使用 Calendar.GetDayOfWeek Calendar.GetWeekOfYear ,而不是写自己。

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

加上XAML中强制性的DataGrid: p>

together with the obligatory DataGrid in the XAML:

    <DataGrid AutoGenerateColumns="true" Name="dataGrid1" />

这篇关于如何确定某个日期的周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆