How can I convert a number between 1 and 7 into a DateTime object in C# which represents the day of the week? The numbers are coming from a XML file which I am parsing. I am retrieving each instance of a field containing a number between 1 and 7 which represents a day of the week between Sunday and Saturday.
如何将1到7之间的数字转换为C#中的DateTime对象,表示星期几?这些数字来自我正在解析的XML文件。我正在检索包含1到7之间的数字的字段的每个实例,它表示星期日和星期六之间的星期几。
4 个解决方案
#1
I would assume casting to a DayOfWeek object would give you a day of the week
我会假设投射到DayOfWeek对象会给你一周的一天
DayOfWeek day = (DayOfWeek)myInt;
As far as a DateTime object goes, the object represents a specific day, not necessarily a random day of the week. You may try adding a # of days to a specific date if this is what you're trying to achieve.
就DateTime对象而言,该对象代表特定的一天,不一定是一周中的随机日。如果这是您要实现的目标,您可以尝试在特定日期添加#天。
http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx
#2
In order to get a DateTime
, you'd need a specific range of dates that you want the weekday to fall under (since a DateTime
is a specific date and time, and a weekday isn't).
为了获得DateTime,您需要一个特定的日期范围,您希望工作日不在其中(因为DateTime是特定的日期和时间,而工作日不是)。
There is a DayOfWeek
enumeration (whose values actually range from 0-6). If all you need is something to represent the day of the week, then you should be able to cast your int to a DayOfWeek
like..
有一个DayOfWeek枚举(其值实际上在0-6之间)。如果您需要的是表示星期几的东西,那么您应该能够将您的int转换为DayOfWeek,例如..
DayOfWeek myDay = (DayOfWeek)yourInt;
If you need an actual DateTime
, you'll need a start date. You could then do...
如果您需要实际的DateTime,则需要一个开始日期。那你可以做......
DateTime myDate = startDate.AddDays(
(int)startDate.DayOfWeek >= yourInt ?
(int)startDate.DayOfWeek - yourInt :
(int)startDate.DayOfWeek - yourInt + 7);
This will give you a DateTime for the next occuring instance of the day of the week you're describing.
这将为您描述的一周中某一天的下一个实例提供DateTime。
#3
DayOfWeek.Sunday is zero, so you could start with an arbitrary fixed date that you know to be Sunday, and add a value between 0 and 6:
DayOfWeek.Sunday为零,因此您可以从您知道星期日的任意固定日期开始,并添加0到6之间的值:
public DateTime GetDayOfWeek(int dayOfWeek)
{
if (dayOfWeek < 0 || dayOfWeek > 6) throw new ArgumentOutOfRangeException(...);
// 4 January 2009 was a Sunday
return new DateTime(2009,1,4).AddDays(dayOfWeek);
}
I'm not sure why you would want this though.
我不知道为什么你会想要这个。
If you only want it to get a localized version of the day of the week as in:
如果您只希望它获得星期几的本地化版本,如:
GetDayOfWeek(3).ToString("dddd"); // Gets name of day of week for current culture
an alternative would be to use DateTimeFormatInfo.DayNames or DateTimeFormatInfo.AbbreviatedDayNames for the culture you want.
另一种方法是将DateTimeFormatInfo.DayNames或DateTimeFormatInfo.AbbreviatedDayNames用于您想要的文化。
#4
A DateTime
instance represents alway a complete date and cannot only represent a day of the week. If the actual date does not matter, take any monday (assuming 0 represents monday) and just add the number of the day.
DateTime实例始终表示完整的日期,并且不仅可以表示一周中的某一天。如果实际日期无关紧要,请采取任何星期一(假设0代表星期一)并只添加当天的号码。
Int32 dayOfWeek = 3;
// date represents a thursday since 2009/04/20 is a monday
DateTime date = new DateTime(2009, 04, 20).AddDays(dayOfWeek);
Else I agree with Adam Robinson's answer - if you just want to hold the day of a week, stick with the DayOfWeek enum (zero is sunday) instead of using an integer.
另外我同意Adam Robinson的回答 - 如果你只想举行一周的日子,坚持使用DayOfWeek枚举(零是星期日)而不是使用整数。
#1
I would assume casting to a DayOfWeek object would give you a day of the week
我会假设投射到DayOfWeek对象会给你一周的一天
DayOfWeek day = (DayOfWeek)myInt;
As far as a DateTime object goes, the object represents a specific day, not necessarily a random day of the week. You may try adding a # of days to a specific date if this is what you're trying to achieve.
就DateTime对象而言,该对象代表特定的一天,不一定是一周中的随机日。如果这是您要实现的目标,您可以尝试在特定日期添加#天。
http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx
#2
In order to get a DateTime
, you'd need a specific range of dates that you want the weekday to fall under (since a DateTime
is a specific date and time, and a weekday isn't).
为了获得DateTime,您需要一个特定的日期范围,您希望工作日不在其中(因为DateTime是特定的日期和时间,而工作日不是)。
There is a DayOfWeek
enumeration (whose values actually range from 0-6). If all you need is something to represent the day of the week, then you should be able to cast your int to a DayOfWeek
like..
有一个DayOfWeek枚举(其值实际上在0-6之间)。如果您需要的是表示星期几的东西,那么您应该能够将您的int转换为DayOfWeek,例如..
DayOfWeek myDay = (DayOfWeek)yourInt;
If you need an actual DateTime
, you'll need a start date. You could then do...
如果您需要实际的DateTime,则需要一个开始日期。那你可以做......
DateTime myDate = startDate.AddDays(
(int)startDate.DayOfWeek >= yourInt ?
(int)startDate.DayOfWeek - yourInt :
(int)startDate.DayOfWeek - yourInt + 7);
This will give you a DateTime for the next occuring instance of the day of the week you're describing.
这将为您描述的一周中某一天的下一个实例提供DateTime。
#3
DayOfWeek.Sunday is zero, so you could start with an arbitrary fixed date that you know to be Sunday, and add a value between 0 and 6:
DayOfWeek.Sunday为零,因此您可以从您知道星期日的任意固定日期开始,并添加0到6之间的值:
public DateTime GetDayOfWeek(int dayOfWeek)
{
if (dayOfWeek < 0 || dayOfWeek > 6) throw new ArgumentOutOfRangeException(...);
// 4 January 2009 was a Sunday
return new DateTime(2009,1,4).AddDays(dayOfWeek);
}
I'm not sure why you would want this though.
我不知道为什么你会想要这个。
If you only want it to get a localized version of the day of the week as in:
如果您只希望它获得星期几的本地化版本,如:
GetDayOfWeek(3).ToString("dddd"); // Gets name of day of week for current culture
an alternative would be to use DateTimeFormatInfo.DayNames or DateTimeFormatInfo.AbbreviatedDayNames for the culture you want.
另一种方法是将DateTimeFormatInfo.DayNames或DateTimeFormatInfo.AbbreviatedDayNames用于您想要的文化。
#4
A DateTime
instance represents alway a complete date and cannot only represent a day of the week. If the actual date does not matter, take any monday (assuming 0 represents monday) and just add the number of the day.
DateTime实例始终表示完整的日期,并且不仅可以表示一周中的某一天。如果实际日期无关紧要,请采取任何星期一(假设0代表星期一)并只添加当天的号码。
Int32 dayOfWeek = 3;
// date represents a thursday since 2009/04/20 is a monday
DateTime date = new DateTime(2009, 04, 20).AddDays(dayOfWeek);
Else I agree with Adam Robinson's answer - if you just want to hold the day of a week, stick with the DayOfWeek enum (zero is sunday) instead of using an integer.
另外我同意Adam Robinson的回答 - 如果你只想举行一周的日子,坚持使用DayOfWeek枚举(零是星期日)而不是使用整数。