C# 中 VB6 的 WeekDay 函数的等效项

发布于 2024-07-11 18:08:01 字数 128 浏览 7 评论 0原文

在 VB6 代码中,我有以下内容:

dim I as Long 

I = Weekday(Now, vbFriday) 

我想要 C# 中的等效内容。 有人可以帮忙吗?

In VB6 code, I have the following:

dim I as Long 

I = Weekday(Now, vbFriday) 

I want the equivalent in C#. Can any one help?

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

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

发布评论

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

评论(4

暖树树初阳… 2024-07-18 18:08:02

是的,每个 DateTime 值都有一个名为 DayOfWeek 的内置属性,它返回同名的枚举...

DayOfWeek dow = DateTime.Now.DayOfWeek;

如果您想要整数值,只需将枚举值转换为 int 即可。

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

你必须添加一个从 1 到 6 的常量,并执行 Mod 7 将其重新调整到除星期日之外的另一天,但是......

Yes, Each DateTime value has a built in property called DayOfWeek that returns a enumeration of the same name...

DayOfWeek dow = DateTime.Now.DayOfWeek;

If you want the integral value just cast the enumeration value to an int.

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

You'll have to add a constant from 1 to 6 and do Mod 7 to realign it to another day besides Sunday, however...

娇女薄笑 2024-07-18 18:08:02

我认为 VB 的 Weekday 函数没有等价的两个参数形式。

你可以使用这样的东西来模拟它;

private static int Weekday(DateTime date, DayOfWeek startDay)
{
    int diff;
    DayOfWeek dow = date.DayOfWeek;
    diff = dow - startDay;
    if (diff < 0)
    {
        diff += 7;
    }
    return diff;
}

然后像这样调用它:

int i = Weekday(DateTime.Now, DayOfWeek.Friday);

今天返回 4,因为星期二是星期五之后的 4 天。

I don't think there is an equivalent of the two argument form of VB's Weekday function.

You could emulate it using something like this;

private static int Weekday(DateTime date, DayOfWeek startDay)
{
    int diff;
    DayOfWeek dow = date.DayOfWeek;
    diff = dow - startDay;
    if (diff < 0)
    {
        diff += 7;
    }
    return diff;
}

Then calling it like so:

int i = Weekday(DateTime.Now, DayOfWeek.Friday);

It returns 4 for today, as Tuesday is 4 days after Friday.

落日海湾 2024-07-18 18:08:01
public static int Weekday(DateTime dt, DayOfWeek startOfWeek)
{
    return (dt.DayOfWeek - startOfWeek + 7) % 7;
}

这可以使用以下方式调用:

DateTime dt = DateTime.Now;
Console.WriteLine(Weekday(dt, DayOfWeek.Friday));

上述输出:

4

因为星期二是星期五之后的 4 天。

public static int Weekday(DateTime dt, DayOfWeek startOfWeek)
{
    return (dt.DayOfWeek - startOfWeek + 7) % 7;
}

This can be called using:

DateTime dt = DateTime.Now;
Console.WriteLine(Weekday(dt, DayOfWeek.Friday));

The above outputs:

4

as Tuesday is 4 days after Friday.

故人如初 2024-07-18 18:08:01

您是指 DateTime.DayOfWeek 属性?

DayOfWeek dow = DateTime.Now.DayOfWeek;

You mean the DateTime.DayOfWeek property?

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