English 中文(简体)
我如何确定某一日期的一周数目?
原标题:How can I determine the week number of a certain date?

我试图用手法制作一个日历。 通过使用纸浆和更多的纸张,我有一个电网,有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;
    }
最佳回答

你们必须使用。 日历.GetDayOfWeekCalendar.Get WeekOf Year ,优于写作。

你们可以保证,如果你写上任何日期/时间处理守则,就会有错,并在不同的地方赢得工作。

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 AutoGenerateColumns="true" Name="dataGrid1" />
问题回答

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

1. 非法定领域、方法或财产需要标注......

如果你在先把哥里安纳塞的话,那么你就可以把“Get WeekOf岁!”

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




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签