Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能知道一个月的哪一天?_C#_Date_Dayofweek_Dayofmonth - Fatal编程技术网

C# 我怎样才能知道一个月的哪一天?

C# 我怎样才能知道一个月的哪一天?,c#,date,dayofweek,dayofmonth,C#,Date,Dayofweek,Dayofmonth,我必须找出给定日期的月份,比如说我有2020年1月26日的日期,现在26日是该月的第4个星期日,我如何才能找到月份中可重复计数的日期?没有直接的方法来实现这一点。您需要调用以下代码 DateTime dateValue = new DateTime(2019, 12, 19); int getDayOfWeek = GetWeekNumberOfMonth(dateValue); //convert to ordinal string ordin

我必须找出给定日期的月份,比如说我有2020年1月26日的日期,现在26日是该月的第4个星期日,我如何才能找到月份中可重复计数的日期?

没有直接的方法来实现这一点。您需要调用以下代码

 DateTime dateValue = new DateTime(2019, 12, 19);
        int getDayOfWeek = GetWeekNumberOfMonth(dateValue);
        //convert to ordinal 
       string ordinalWeekOfMonth = AddOrdinal(getDayOfWeek);
      Console.WriteLine(ordinalWeekOfMonth + " " + dateValue.DayOfWeek);//output like 2nd sunday
以下依赖方法可用于调用上述代码

     public static int GetWeekNumberOfMonth(DateTime date)
    {
        date = date.Date;
        DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1);
        DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
        if (firstMonthMonday > date)
        {
            firstMonthDay = firstMonthDay.AddMonths(-1);
            firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
        }
        return (date - firstMonthMonday).Days / 7 + 1;
    }

    public static string AddOrdinal(int num)
    {
        if (num <= 0) return num.ToString();

        switch (num % 100)
        {
            case 11:
            case 12:
            case 13:
                return num + "th";
        }

        switch (num % 10)
        {
            case 1:
                return num + "st";
            case 2:
                return num + "nd";
            case 3:
                return num + "rd";
            default:
                return num + "th";
        }

    }
public static int GetWeekNumberOfMonth(日期时间日期)
{
日期=日期。日期;
DateTime firstMonthDay=新的日期时间(date.Year,date.Month,1);
DateTime firstMonthMonday=firstMonthDay.AddDays((DayOfWeek.Monday+7-firstMonthDay.DayOfWeek)%7);
如果(第一个月星期日>日期)
{
firstMonthDay=firstMonthDay.AddMonths(-1);
firstMonthMonday=firstMonthDay.AddDays((DayOfWeek.Monday+7-firstMonthDay.DayOfWeek)%7);
}
返回(日期-第一个月的星期一)。天/7+1;
}
公共静态字符串AddOrdinal(int num)
{

如果(num)你问的是什么语言?我需要C语言。