如何更改哪一天是一周的第一天?

时间:2011-10-09 12:13:45

标签: c# asp.net .net datetime

我需要在asp.net中更改一周的第一天,即我希望星期六成为一周的第一天。

例如,下面的代码应该在星期一返回3:

(int)DateTime.Now.DayOfWeek

5 个答案:

答案 0 :(得分:7)

您无法影响root@kali:~# cd ~/.buildozer/android/platform/python-for-android root@kali:~/.buildozer/android/platform/python-for-android# ls COPYING dist docs README.rst src cythonizer.py distribute.sh LICENSE recipes tools root@kali:~/.buildozer/android/platform/python-for-android# ./distribute.sh -m "pil ffmpeg kivy" Check build dependencies for Kali Avoid check build dependencies, unknow platform Kali Check environment No ANDROIDSDK environment set, abort root@kali:~/.buildozer/android/platform/python-for-android# 的值,因为这是一种DateTime.DayOfWeek类型,它是一个枚举(即值是常量)。 System.DayOfWeek的定义位于下面的代码块中。因此,如果您希望将System.DayOfWeek视为星期一和星期六DayOfWeek作为一周的第一天,那么我必须假设您需要一个基于1到7的编号系统。在这种情况下,您可以3获取所需的数字。如果您不需要进行此计算,最好只将((int)DateTime.Now.DayOfWeek+1) % 7 + 1的值与其枚举常量(例如DateTime.Now.DayOfWeek)进行比较。

if( DateTime.Now.DayOfWeek == DayOfWeek.Monday ) ...

答案 1 :(得分:4)

使用以下代码更改asp.net应用程序中的第一天。

CultureInfo _culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
CultureInfo _uiculture = (CultureInfo)CultureInfo.CurrentUICulture.Clone();

_culture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday;
_uiculture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday;

System.Threading.Thread.CurrentThread.CurrentCulture = _culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = _uiculture;

答案 2 :(得分:1)

我想你只需要改变你使用的文化(DayOfWeek Enumeration)。

Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-EG")

答案 3 :(得分:1)

我创建了这个C#扩展方法,该方法根据替代的第一个工作日返回从零开始的索引。

DayOfWeek默认为星期日= 0,星期一= 1,...,星期六= 6.我们希望(例如)DayOfWeek为星期一= 0,星期二= 1,... ,星期日= 6。

鉴于当前的工作日是星期天:

DateTime.Now.DayOfWeek; // returns Sunday
(int)DateTime.Now.DayOfWeek; // returns 0
DateTime.Now.DayOfWeek(DayOfWeek.Monday); // returns 6    

public static class ExtensionMethods
{
    /// <summary>
    /// Returns an zero-based index where firstDayOfWeek = 0 and lastDayOfWeek = 6
    /// </summary>
    /// <param name="value"></param>
    /// <param name="firstDayOfWeek"></param>
    /// <returns>int between 0 and 6</returns>
    public static int DayOfWeek(this DateTime value, DayOfWeek firstDayOfWeek)
    {
        var idx = 7 + (int)value.DayOfWeek - (int)firstDayOfWeek;
        if (idx > 6) // week ends at 6, because Enum.DayOfWeek is zero-based
        {
            idx -= 7;
        }
        return idx;
    }
}

答案 4 :(得分:0)

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.Date.DayOfWeek == DayOfWeek.Friday)
            {
                e.Day.IsSelectable = false;
                e.Cell.BackColor = Color.AliceBlue;
            }
        }

它确实有效。这里的DayRender是一个事件。