如何确定某一日期的周数? [英] How can I determine the week number of a certain date?
问题描述
我试图让使用WPF的日历。通过使用itemsPanel多,我有7列(周日至周六)和6行(每月星期#)的网格。如果我可以通过获取(月)平日和一周号码查询每月的第一天的首发位置,我怎么能找到周数(每月0-5)?也不能不知何故,我只是在日历填补呢?我迷路了,我不知道还有什么尝试。
公共部分类SchedulePage:页
{
主窗口_parentForm;
公众诠释星期几; 公共SchedulePage(主窗口parentForm)
{
的InitializeComponent();
_parentForm = parentForm;
//日期时间日期=新的日期时间(年,月,日); _parentForm.bindings =新BindingCamper();
_parentForm.bindings.schedule.Add(新附表{WeekNo =(INT)getWeekNumber(),工作日=星期几});
的DataContext = _parentForm.bindings;
// lblTest.Content =日期(2011,10,27);
} 公共双getWeekNumber()
{
DAYOFWEEK = getWeekDay(2011,10,31);
双H =星期几/ 7;
双G = Math.Floor(H);
返回克;
} 公众诠释getWeekDay(INT年,月整型,诠释天)
{
//年= 2011;
//月= 10;
//天= 27;
INT [] T = {0,3,2,5,0,3,5,1,4,6,2,4};
//年 - =月< 3;
收益率(年+年/ 4 - 年/ 100 +年/ 400 + T [月 - 1] +天),7%;
}
您必须使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getdayofweek.aspx\">Calendar.GetDayOfWeek和<一个href=\"http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx\">Calendar.GetWeekOfYear在preference编写你自己。
您可以保证,如果你写的任何日期/时间处理code自己它将包含错误,并且不会在不同的地区工作。
公共类行
{
公共字符串MonthWeek {搞定;组; }
公共字符串年{搞定;组; }
公共字符串月{搞定;组; }
公共字符串日{搞定;组; }
公共字符串WEEKOFYEAR {搞定;组; }
}公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent();
变种L =新的List&LT;行&GT;();
日期时间的startDate = DateTime.Now;
日期时间D =新日期时间(startDate.Year,startDate.Month,1);
VAR CAL = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
变种毫秒= cal.GetWeekOfYear(新日期时间(d.Year,d.Month,1),System.Globalization.CalendarWeekRule.FirstDay,System.DayOfWeek.Sunday);
为(变量I = 1; d.Month == startDate.Month; D = d.AddDays(1))
{
变种SI =新行();
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 = 1;
}
}
在XAML中强制性的DataGrid在一起:
&LT;数据网格的AutoGenerateColumns =真NAME =DataGrid1中/&GT;
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;
}
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" />
这篇关于如何确定某一日期的周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!