C#中如何写天数

how to write number of days in c#

如何用 C# 编写程序来显示日期

例如:

我让用户输入一天,他写星期一,我想给他看 2

尝试使用DateTime.Now.DayOfWeek。定义:

namespace System
{
    using System.Runtime.InteropServices;

    [Serializable, ComVisible(true), __DynamicallyInvokable]
    public enum DayOfWeek
    {
        [__DynamicallyInvokable]
        Friday = 5,
        [__DynamicallyInvokable]
        Monday = 1,
        [__DynamicallyInvokable]
        Saturday = 6,
        [__DynamicallyInvokable]
        Sunday = 0,
        [__DynamicallyInvokable]
        Thursday = 4,
        [__DynamicallyInvokable]
        Tuesday = 2,
        [__DynamicallyInvokable]
        Wednesday = 3
    }
}

示例:

// For your case Monday == 2
int todayDayOfWeekNumber = (int)DateTime.Now.DayOfWeek + 1;

试试这个:

(System.DateTime.Now.Day) % 7;     //will return integer 2 for Monday

示例:

DateTime inputDate = System.DateTime.Now;
System.Windows.Forms.MessageBox.Show(((inputDate.Day)%7).ToString());

或:

使用 DayOfWeek(就像提到的 dyatchenko):

((int)DateTime.Now.DayOfWeek + 1)

DayOfWeek 从周日开始(0)。因此,如果您想在星期一获得 2,只需添加 1。