我使用以下方法检索一周。
public int GetWeek(DateTime date)
{
var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
如果周数在0到9之间,它应该返回00 - 09作为周数。 我该怎么办?
答案 0 :(得分:1)
我认为,当您正在寻找前导零时,您希望格式化这些数字。
如果是这样,只需使用int.ToString()来格式化GetWeek方法:
public int GetWeek(DateTime date)
{
var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
public string GetWeekFormatted(DateTime date)
{
return GetWeek(date).ToString("00");
}