如何获取星期几的整数值

发布于 2025-01-03 08:24:40 字数 282 浏览 2 评论 0原文

如何以整数格式获取一周中的某一天?我知道 ToString 只会返回一个字符串。

DateTime ClockInfoFromSystem = DateTime.Now;
int day1;
string day2;
day1= ClockInfoFromSystem.DayOfWeek.ToString(); /// it is not working
day2= ClockInfoFromSystem.DayOfWeek.ToString(); /// it gives me string

How do I get the day of a week in integer format? I know ToString will return only a string.

DateTime ClockInfoFromSystem = DateTime.Now;
int day1;
string day2;
day1= ClockInfoFromSystem.DayOfWeek.ToString(); /// it is not working
day2= ClockInfoFromSystem.DayOfWeek.ToString(); /// it gives me string

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

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

发布评论

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

评论(10

守不住的情 2025-01-10 08:24:41

以字符串形式获取 Enum(例如 DayOfWeek)的整数值的正确方法是:

DayOfWeek.ToString("d")

The correct way to get the integer value of an Enum such as DayOfWeek as a string is:

DayOfWeek.ToString("d")
半世蒼涼 2025-01-10 08:24:41

试试这个。它会工作得很好:

int week = Convert.ToInt32(currentDateTime.DayOfWeek);

Try this. It will work just fine:

int week = Convert.ToInt32(currentDateTime.DayOfWeek);
拥抱影子 2025-01-10 08:24:41

另一种获取星期一(整数值 1)和星期日(整数值 7)的方法

int day = ((int)DateTime.Now.DayOfWeek + 6) % 7 + 1;

Another way to get Monday with integer value 1 and Sunday with integer value 7

int day = ((int)DateTime.Now.DayOfWeek + 6) % 7 + 1;
梦毁影碎の 2025-01-10 08:24:41

正确答案,确实是获取int值的正确答案。

但是,如果您只是检查以确保今天是星期日...请考虑使用以下代码,而不是转换为 int。这提供了更多的可读性。

if (yourDateTimeObject.DayOfWeek == DayOfWeek.Sunday)
{
    // You can easily see you're checking for sunday, and not just "0"
}

The correct answer, is indeed the correct answer to get the int value.

But, if you're just checking to make sure it's Sunday for example... Consider using the following code, instead of casting to an int. This provides much more readability.

if (yourDateTimeObject.DayOfWeek == DayOfWeek.Sunday)
{
    // You can easily see you're checking for sunday, and not just "0"
}
烟雨凡馨 2025-01-10 08:24:41
DateTime currentDateTime = DateTime.Now;
int week = (int) currentDateTime.DayOfWeek;
DateTime currentDateTime = DateTime.Now;
int week = (int) currentDateTime.DayOfWeek;
深海少女心 2025-01-10 08:24:41

可读性很重要。

如果您需要一个整数:

int day1 = (int)ClockInfoFromSystem.DayOfWeek;

如果您需要一个工作日整数字符串:

string daystr = $"{(int)ClockInfoFromSystem.DayOfWeek}"; // Unambiguous string of int.

不要使用推荐的 ToString 转换,因为大多数程序员必须查找它以确保它是一个整数字符串,而不是一个月中的某一天。真的是微软吗?

string daystr = ClockInfoFromSystem.DayOfWeek.ToString("d"); // Whaa? Horrible! Don't do this.

要更改为一周的开始,请添加从星期日起的天数 mod 7。从星期日开始倒数以获得天数,例如从星期日倒数 1 是星期六,从星期日倒数 2 是星期五,等等。

int satStart = (int)(ClockInfoFromSystem.DayOfWeek + 1) % 7; // Saturday start
int monStart = (int)(ClockInfoFromSystem.DayOfWeek + 6) % 7; // Monday start

Readability counts.

If you need an integer:

int day1 = (int)ClockInfoFromSystem.DayOfWeek;

If you need a string of the weekday integer:

string daystr = 
quot;{(int)ClockInfoFromSystem.DayOfWeek}"; // Unambiguous string of int.

Do not use the recommended ToString conversion, because the majority of programmers are going to have to look it up to make sure that it's a string of the integer and not day of month. Really Microsoft?

string daystr = ClockInfoFromSystem.DayOfWeek.ToString("d"); // Whaa? Horrible! Don't do this.

To change to start of week, add the number of days from Sunday mod 7. Count backwards from Sunday to get the number of days, e.g. 1 back from Sunday is Saturday, 2 back from Sunday is Friday, etc.

int satStart = (int)(ClockInfoFromSystem.DayOfWeek + 1) % 7; // Saturday start
int monStart = (int)(ClockInfoFromSystem.DayOfWeek + 6) % 7; // Monday start
请恋爱 2025-01-10 08:24:40

使用

day1 = (int)ClockInfoFromSystem.DayOfWeek;

Use

day1 = (int)ClockInfoFromSystem.DayOfWeek;
那一片橙海, 2025-01-10 08:24:40
int day = (int)DateTime.Now.DayOfWeek;

一周的第一天:星期日(值为零)

int day = (int)DateTime.Now.DayOfWeek;

First day of the week: Sunday (with a value of zero)

来日方长 2025-01-10 08:24:40

如果要将一周的第一天设置为整数值 1 的星期一和整数值 7 的星期日

int day = ((int)DateTime.Now.DayOfWeek == 0) ? 7 : (int)DateTime.Now.DayOfWeek;

If you want to set first day of the week to Monday with integer value 1 and Sunday with integer value 7

int day = ((int)DateTime.Now.DayOfWeek == 0) ? 7 : (int)DateTime.Now.DayOfWeek;
岁月无声 2025-01-10 08:24:40
day1= (int)ClockInfoFromSystem.DayOfWeek;
day1= (int)ClockInfoFromSystem.DayOfWeek;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文