问题
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