如何在给定月份和年份的特定周内获得dates
?
例如:
我的参数:
June - 2013 - The second week
我想要这样的结果集:
9-6-2013
10-6-2013
11-6-2013
12-6-2013
13-6-2013
我希望它从Sun开始到星期四。
答案 0 :(得分:7)
我的Noda Time库中我会:
foreach
循环):所以:
IEnumerable<LocalDate> GetSundayToWednesday(int year, int month, int week)
{
LocalDate date = new LocalDate(year, month, 1).PlusDays(-1);
for (int i = 0; i < week; i++)
{
date = date.Next(IsoDayOfWeek.Sunday);
}
// You always want 4 days, Sunday to Wednesday
for (int i = 0; i < 4; i++)
{
yield return date;
date = date.PlusDays(1);
}
}
仅使用DateTime
,我可能会在本月的第一天开始可以(周* 7 + 1)并循环直到我到达正确的一天一周,然后从那里开始:
IEnumerable<DateTime> GetSundayToWednesday(int year, int month, int week)
{
// Consider breaking this part out into a separate method?
DateTime date = new DateTime(year, month, week * 7 + 1);
for (int i = 0; i < 7; i++)
{
if (date.DayOfWeek == DayOfWeek.Sunday)
{
break;
}
date = date.AddDays(1);
}
// You always want 4 days, Sunday to Wednesday
for (int i = 0; i < 4; i++)
{
yield return date;
date = date.AddDays(1);
}
}
像这样循环并不是非常有效 - 你可以计算出推进的天数 - 但更明显正确。您可以通过更有效的方法轻松地以一个一个错误(或回到上个月)结束。当然,如果这很重要,你可以选择付出更多努力来提高效率。
答案 1 :(得分:1)
我还没有真正测试过这个,但我认为这或多或少都是你想要的:
int year = 2013;
int month = 6;
int lookupWeek = 2;
int daysInMonth = DateTime.DaysInMonth(year, month);
int weekCounter = 1;
List<DateTime> weekDays = new List<DateTime>();
for (int day = 1; day <= daysInMonth; day++)
{
DateTime date = new DateTime(year,month,day);
if(date.DayOfWeek == DayOfWeek.Sunday && day > 1) weekCounter++;
if(weekCounter == lookupWeek) weekDays.Add(date);
}
答案 2 :(得分:1)
此方法提供一个月中特定周的日期:
static IEnumerable<string> DaysInWeek(int year, int month, int week)
{
var date = new DateTime(year, month, 1);
var calendar = new GregorianCalendar();
var firstWeek = calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday);
var days = calendar.GetDaysInMonth(year, month);
var daysInWeek = (from day in Enumerable.Range(0, calendar.GetDaysInMonth(year, month) - 1)
let dayDate = date.AddDays(day)
let week2 = calendar.GetWeekOfYear(dayDate, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday) - firstWeek + 1
where week2 == week
select day + 1).ToList();
foreach (var d in daysInWeek) yield return string.Format("{0:00}-{1:00}-{2:0000}", d, month, year);
}
这个的输出:
foreach (var d in DaysInWeek(week, year, month).Take(5)) Log.Info(d);
将是:
09-06-2013
10-06-2013
11-06-2013
12-06-2013
13-06-2013
注意:我编辑了代码。有一个小虫子;因为在很多个月里,第一周和上周并不是完整的一周,而那一周的某些日子属于另一个月。
答案 3 :(得分:1)
如果您对客户端(JavaScript)路由感兴趣,那么date.js可能值得一看,它允许以下内容:
// What date is next thursday?
Date.today().next().thursday();
// Add 3 days to Today
Date.today().add(3).days();
// Is today Friday?
Date.today().is().friday();
// Number fun
(3).days().ago();
// 6 months from now
var n = 6;
n.months().fromNow();
// Set to 8:30 AM on the 15th day of the month
Date.today().set({ day: 15, hour: 8, minute: 30 });
// Convert text into Date
Date.parse('today');
Date.parse('t + 5 d'); // today + 5 days
Date.parse('next thursday');
Date.parse('February 20th 1973');
Date.parse('Thu, 1 July 2004 22:30:00');
我发现自然语言语法(即“下周四”)非常强大。
答案 4 :(得分:1)
如果你热衷于使用linq(我几乎总是!),你可以这样做:
int year = 2013;
int month = 6;
int weekOfMonth = 2;
var dates = Enumerable.Range(1, DateTime.DaysInMonth(year, month))
.Select(day => new DateTime(year, month, day))
.GroupBy(g=> g.DayOfYear/7)
.ToList();
var week = dates.Min(g => g.Key) + weekOfMonth - 1;
var result = dates.Where(g=> g.Key.Equals(week)).Select(g => g.ToList());
答案 5 :(得分:0)
试试这个
protected void button_click(object sender, EventArgs e)
{
DateTime dt = new DateTime(2013, 6, 1);
string[] dates = getDates(dt);
}
public string[] getDates(DateTime dt)
{
string[] result = new string[7]; ;
for (int i = getDay(dt.DayOfWeek.ToString()); i < 7; i++)
{
if (i == 0)
{
break;
}
else
{
dt = dt.AddDays(1);
}
}
for (int i = 0; i < 7; i++)
{
dt = dt.AddDays(1);
result[i] = dt.ToShortDateString();
}
return result;
}
public int getDay(string day)
{
switch (day)
{
case "Monday":
return 0;
case "Tuesday":
return 1;
case "Wednesday":
return 2;
case "Thursday":
return 3;
case "Friday":
return 4;
case "Saturday":
return 5;
case "Sunday":
return 6;
default:
return 0;
}
}## Heading ##