表示星期几的 DateTime 对象

发布于 2024-07-18 11:43:48 字数 123 浏览 5 评论 0原文

如何将 1 到 7 之间的数字转换为 C# 中代表星期几的 DateTime 对象? 这些数字来自我正在解析的 XML 文件。 我正在检索包含 1 到 7 之间的数字的字段的每个实例,该数字代表星期日到星期六之间的一周中的一天。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一口甜 2024-07-25 11:43:48

我假设转换为 DayOfWeek 对象将为您提供一周中的一天

DayOfWeek day = (DayOfWeek)myInt;

就 DateTime 对象而言,该对象代表特定的一天,不一定是一周中的随机一天。 如果您想要实现这一目标,您可以尝试在特定日期中添加天数。

http://msdn.microsoft.com/ en-us/library/system.dayofweek.aspx

I would assume casting to a DayOfWeek object would give you a day of the week

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.

http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx

べ繥欢鉨o。 2024-07-25 11:43:48

为了获取 DateTime,您需要希望工作日属于特定的日期范围(因为 DateTime 是特定的日期和时间,并且工作日则不然)。

有一个 DayOfWeek 枚举(其值实际上范围为 0-6)。 如果您需要的只是代表星期几,那么您应该能够将 int 转换为 DayOfWeek ,例如......

DayOfWeek myDay = (DayOfWeek)yourInt;

如果您需要实际的 DateTime,你需要一个开始日期。 然后您可以执行以下操作...

DateTime myDate = startDate.AddDays(
    (int)startDate.DayOfWeek >= yourInt ? 
        (int)startDate.DayOfWeek - yourInt : 
        (int)startDate.DayOfWeek - yourInt + 7);

这将为您提供您所描述的一周中的下一个发生实例的日期时间。

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).

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 myDay = (DayOfWeek)yourInt;

If you need an actual DateTime, you'll need a start date. You could then do...

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.

浪漫之都 2024-07-25 11:43:48

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);
}

但我不确定您为什么需要这个。

如果您只想获取星期几的本地化版本,如下所示:

GetDayOfWeek(3).ToString("dddd"); // Gets name of day of week for current culture

另一种方法是使用 DateTimeFormatInfo.DayNames 或 DateTimeFormatInfo.AbbreviatedDayNames 作为您想要的区域性。

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:

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.

心奴独伤 2024-07-25 11:43:48

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);

否则我同意 Adam Robinson 的回答 - 如果您只想保留一周中的某一天,请坚持使用 DayOfWeek 枚举(零是星期日)而不是使用整数。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文