How can I determine the week number of a certain date?

后端 未结 3 2095
小鲜肉
小鲜肉 2020-11-30 11:38

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

相关标签:
3条回答
  • 2020-11-30 12:06

    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
        );
    }
    
    0 讨论(0)
  • 2020-11-30 12:13

    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;
    }
    
    0 讨论(0)
  • 2020-11-30 12:19

    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" />
    
    0 讨论(0)
提交回复
热议问题