Week number of a year in dateformat

橙三吉。 提交于 2019-12-01 14:39:22

问题


I need to get the year I use the format YYYY, and month MM, and so on. I am looking to see if there is a format to get the week number of the year.

The date that I am using as example is 2008-03-09 16:05:07.000, and I would like to know the week number which is 11 in this case. Is there a way to retrieve 11 programmatically?


回答1:


There's no format that provides the weeknumber, but you can get it in this way easily:

System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
int weekNum = cul.Calendar.GetWeekOfYear(
    DateTime.Now, 
    System.Globalization.CalendarWeekRule.FirstFourDayWeek, 
    DayOfWeek.Monday);
  • CultureInfo.Calendar Property
  • Calendar.GetWeekOfYear Method
  • Custom Date and Time Format Strings

To answer your comment how to get the quarter of a year of a given DateTime:

int quarter = (int)((DateTime.Now.Month - 1) / 3) + 1


来源:https://stackoverflow.com/questions/9592650/week-number-of-a-year-in-dateformat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!