我需要在asp.net中更改一周的第一天,即我希望星期六成为一周的第一天.
例如,下面的代码应该在星期一返回3:
(int)DateTime.Now.DayOfWeek
解决方法
您不能影响DateTime.DayOfWeek的值,因为这是一种System.DayOfWeek,它是一个枚举(即值是常量). System.DayOfWeek的定义在下面的代码块中.所以,如果你想把DayOfWeek视为星期一和星期六的3是一周的第一天,那么我必须假设你想要一个基于1到7的编号系统.在这种情况下,你可以做((int)DateTime.Now.DayOfWeek 1)%7 1来获得你想要的数字.如果您不需要这个用于计算,最好只将DateTime.Now.DayOfWeek的值与其枚举常量进行比较(例如if(DateTime.Now.DayOfWeek == DayOfWeek.Monday)…).
// Summary: // Specifies the day of the week. [Serializable] [ComVisible(true)] public enum DayOfWeek { // Summary: // Indicates Sunday. Sunday = 0,// // Summary: // Indicates Monday. Monday = 1,// // Summary: // Indicates Tuesday. Tuesday = 2,// // Summary: // Indicates Wednesday. Wednesday = 3,// // Summary: // Indicates Thursday. Thursday = 4,// // Summary: // Indicates Friday. Friday = 5,// // Summary: // Indicates Saturday. Saturday = 6,}