/// <summary>
/// 4位数字年周转8位数字年月日 2301->20230101
/// </summary>
public static string GetFirstDayOfWeek(string strYearWeek, System.Globalization.CultureInfo culture)
{
if (strYearWeek.Length != 4) throw new Exception("GetFirstDayOfWeek() strYearWeek 长度必须是4位");
if (strYearWeek.IsNotNumeric()) throw new Exception("GetFirstDayOfWeek() strYearWeek 必须是数字");
int year = ("20" + strYearWeek.Substring(0, 2)).ToInt();
int week = strYearWeek.Substring(2).ToInt();
System.Globalization.Calendar calendar = culture.Calendar;
DateTime firstOfYear = new DateTime(year, 1, 1, calendar);
DateTime targetDay = calendar.AddWeeks(firstOfYear, week - 1);
return targetDay.Year.ToString("0000") + targetDay.Month.ToString("00") + targetDay.Day.ToString("00");
}4位数字年周转8位数字年月日 2301->20230101
于 2023-02-09 08:18:09 首次发布
该代码段提供了一个公共静态方法,用于将4位表示的年周转换为8位表示的年月日。它首先检查输入字符串的格式,然后计算出一年中的第几周,最后利用.NET的Calendar类确定该周的第一天并返回8位格式的日期。
460

被折叠的 条评论
为什么被折叠?



