我需要在currentMonth
中从DayNames创建一个数组DateTime dt = new DateTime();
dt = DateTime.Now;
string[] days = ( names of days); // this month should be: Sunday, Monday...
我该怎么做,请
答案 0 :(得分:3)
LINQ方式获取当月的所有日期名称,从“星期日”开始(例如,在en-us中):
var now = DateTime.Now;
var culture = System.Globalization.CultureInfo.CurrentCulture;
String[] thisMonthDays =
Enumerable.Range(1, culture.Calendar.GetDaysInMonth(now.Year, now.Month))
.Select(day => new DateTime(now.Year, now.Month, day))
.Select(date => culture.DateTimeFormat.GetDayName(date.DayOfWeek))
.ToArray();
(因为我来自德国,所以列表以“Sonntag”开头)
答案 1 :(得分:2)
您好,您可以查找DayOfWeek enum http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx
var arr = Enum.GetValues(typeof(DayOfWeek));
答案 2 :(得分:2)
你可以这样做:
DateTime dt = DateTime.Now;
DateTime end_DateTime =
new DateTime(dt.Year, dt.Month, DateTime.DaysInMonth(dt.Year, dt.Month));
List<string> result = new List<string>();
for (DateTime dt1 = new DateTime(dt.Year, dt.Month, 1);
dt1 <= end_DateTime; dt1 = dt1.AddDays(1))
{
result.Add(dt1.DayOfWeek.ToString());
}
最后
string[] resultarr = result.ToArray();
这会给你
"Sunday", "Monday", "Tuesday" .... and so on for current month of July.
因为01/07/2012 (dd/MM/yyyy)
是星期天等等。
答案 3 :(得分:2)
DateTime Base = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1);
int x = DateTime.DaysInMonth(Base.Year, Base.Month);
string[] daysOfMonth = new string[x];
int i = 0;
while (Base.Day != x)
{
daysOfMonth[i++] = Base.DayOfWeek.ToString();
Base = Base.AddDays(1);
}
答案 4 :(得分:1)
我认为你可以这样做
DateTime dt = new DateTime();
dt = DateTime.Now;
string[] days1 = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; // this month should be: Sunday, Monday...
希望这会对你有所帮助: - )
答案 5 :(得分:1)
试试这个:
DateTime dt = new DateTime(year, month,...); // initialize to first day in month
List<string> days = new List<string>();
do
{
days.Add(dt.ToString("dddd"));
dt.AddDays(1);
} while (dt.Month == month);
答案 6 :(得分:1)
来自你的上一个帖子
DateTime Base = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1);
List<String> Days = new List<String>();
int x = DateTime.DaysInMonth(Base.Year, Base.Month);
while (Base.Day != x)
{
Days.Add(Base.DayOfWeek.ToString());
Base = Base.AddDays(1);
}
然后使用
String[] StringArray = Days.ToArray();
如果你需要和字符串数组而不是列表。
除非你真的需要一个数组,否则我会保持列表的原样,它可以以相同的方式使用,所以我看不出有必要做一个数组副本的好理由。
或者你可以这样做(正如Habib.OSU建议的那样):
DateTime Base = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1);
int x = DateTime.DaysInMonth(Base.Year, Base.Month);
String[] StringArray = new String[x];
while (Base.Day != x)
{
daysOfMonth[Base.Day - 1] = Base.DayOfWeek.ToString();
Base = Base.AddDays(1);
}
这很可能是两者中的更快。
答案 7 :(得分:0)
一周中的几天总是相同的,你不需要将它们与当前月份联系起来。
如果你只关心用英语表达。
string[] weekdays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Saturday" };
如果您想要一个月内所有日期的列表,请参阅此thread。