English 中文(简体)
在一个月里表现第一周的最合法方式是什么
原标题:What is the most elegant way of showing first week in a month

在C#中,我想在日历上显示第一个星期(在html表格中),我正试图列出产生正确天数的最廉价算法。

如果该周的第一天不是星期日,我想显示上个月的时间(如你在固定日历上看到的情况)。 因此,作为你本月的投入。 5月。 我愿提出:

www.un.org/Depts/DGACM/index_spanish.htm

 <table>
 <tr>
   <th>S</th>
   <th>M</th>
   <th>T</th>
   <th>W</th>
   <th>TH</th>
   <th>F</th>
   <th>Sa</th>
 </tr>
 <tr>
   <td>25</td>
   <td>26</td>
   <td>27</td>
   <td>28</td>
   <td>29</td>
   <td>30</td>
   <td>1</td>
 </tr></table>

这样,它就应当显示这样的情况(协调)。

S | M | T | W | Th | F | Sa |
25 - 26 - 27 - 28 - 29 - 30 - 1

鉴于每个月的开始时间将不同,我试图用时间标出获取这一数据价值的一种合法办法。 我看到,它有一天的财产。

我在服务器上的C#中制作了这个表,以压倒到一个html页。

最佳回答

您可使用以下法典,以达到第一周:

public static IEnumerable<DateTime> GetFirstWeek(int year, int month) {
    DateTime firstDay = new DateTime(year, month, 1);
    firstDay = firstDay.AddDays(-(int) firstDay.DayOfWeek);
    for (int i = 0; i < 7; ++i)
        yield return firstDay.AddDays(i);
}
问题回答
public static IEnumerable<DateTime> GetFirstWeek(int month, int year)
    {
        var firstDay = new DateTime(year, month, 1);
        var dayOfWeek = firstDay.DayOfWeek;
        var firstWeekDay = firstDay.AddDays(-(int)dayOfWeek);

        for (int i = 0; i < 7; i++)
        {
            yield return firstWeekDay.AddDays(i);
        }
    }

这一工作应当:

DateTime date = new DateTime(year, month, 1);
DayOfWeek firstDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
int daysBack = (7 + date.DayOfWeek - firstDay) % 7;

Enumerable.Range(-daysBack, 7)
    .Select(x => date.AddDays(x))
    .ToList()
    .ForEach(d => 
    {
        // a place for html generation
    });

这里只是这样做的一个简单方式。

    //Code not tested thoroughly.
    private DateTime[] GetWeek(int month)
    {
        DateTime firstDayofMonth = new DateTime(DateTime.Now.Year, month, 1);
        if (firstDayofMonth.DayOfWeek == DayOfWeek.Sunday)
            return GetWeek(firstDayofMonth);
        else
        {
            DateTime sundayOfPreviousMonth = firstDayofMonth;
            do
            {
                sundayOfPreviousMonth = sundayOfPreviousMonth.AddDays(-1);
            } while (sundayOfPreviousMonth.DayOfWeek != DayOfWeek.Sunday);
            return GetWeek(sundayOfPreviousMonth);
        }
    }

    private DateTime[] GetWeek(DateTime date)
    {
        if (date.DayOfWeek != DayOfWeek.Sunday)
            throw new ArgumentException("Invalid weekday.");
        DateTime[] week = new DateTime[7];
        for (int i = 0; i < week.Length; i++)
        {
            week[i] = date.AddDays(i);
        }
        return week;
    }

检查该法典:

var days = new[]
{
    "S", "M", "T", "W", "TH", "F", "Sa"
};
var date = DateTime.Parse( "01.05.2010" );
var dayOfWeek = (int)date.DayOfWeek;

for( var i = 0; i < 7; i++ )
{
    Console.WriteLine( days[i] + ": " + date.AddDays( i - dayOfWeek ) );
}

这只是给你一个想法——例如,如果你利用文化信息来限定你的日期。 现在我把任何日期作为投入使用,但你也可以将其削减到每个月的第一天。





相关问题
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 ...

热门标签