获取给定月份和指定月份的开始和结束日期。给定的一周 [英] Get the start and end date of a given week year, given month & given week
本文介绍了获取给定月份和指定月份的开始和结束日期。给定的一周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在c#4.0中获得给定年份(int),给定月份(int)和给定星期(int)的开始和结束日期{例如年:2011月:07周:04}?
How can I get the start and end date of a given year (int), given month (int), and given week (int) { example Year : 2011 Month: 07 week: 04 } in c# 4.0? Thanks in advance.
该年度的开始日期为2011年7月,该月的周号为04。
The Start Date of the Year 2011 Month 07 and the week number of the month is 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, dateTime.Month, 1);
return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}
多年来您可以做类似的事情:
You can do something similar for years:
DateTime time = new DateTime(2011,1,1);
time.AddYears(1).AddDays(-1);
然后 week 需要使用CultureInfo.FirstDay(或某些情况下要设置为一周的第一天)国家/地区(星期一,有时是星期日)。
And week needs to use the CultureInfo.FirstDay (or whatever you want to set as the first day of a week, in some countries it's Monday, sometimes it's Sunday).
/// <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;
}
这篇关于获取给定月份和指定月份的开始和结束日期。给定的一周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文