2011年07月的开始日期和月份的周数是04.
解决方法 Google是你的朋友.月:
public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime){ return new DateTime(dateTime.Year,dateTime.Month,1);}public DateTime LastDayOfMonthFromDateTime(DateTime dateTime){ DateTime firstDayOfTheMonth = new DateTime(dateTime.Year,1); return firstDayOfTheMonth.AddMonths(1).AddDays(-1);}
您可以多年来做类似的事情:
DateTime time = new DateTime(2011,1,1); time.AddYears(1).AddDays(-1);
week需要使用CultureInfo.FirstDay(或者你想要设置为一周的第一天,在某些国家是星期一,有时是星期天).
/// <summary> /// Returns the first day of the week that the specifIEd /// date is in using the current culture. /// </summary> public static DateTime GetFirstDayOfWeek(DateTime dayInWeek) { CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture; return GetFirstDateOfWeek(dayInWeek,defaultCultureInfo); } /// <summary> /// Returns the first day of the week that the specifIEd date /// is in. /// </summary> public static DateTime GetFirstDayOfWeek(DateTime dayInWeek,CultureInfo cultureInfo) { DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek; DateTime firstDayInWeek = dayInWeek.Date; while (firstDayInWeek.DayOfWeek != firstDay) firstDayInWeek = firstDayInWeek.AddDays(-1); return firstDayInWeek; }总结
以上是内存溢出为你收集整理的c# – 获取给定周年,给定月份和给定周的开始和结束日期全部内容,希望文章能够帮你解决c# – 获取给定周年,给定月份和给定周的开始和结束日期所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)