C# 如何获取某一年某个月的某一天的日期?

C# 如何获取某一年某个月的某一天的日期?,c#,sql-server-2005,C#,Sql Server 2005,我想在sql Server 2005或c中获取日期,例如2011年9月的星期日 所以结果应该是 4/9/2011 11/9/2011 18/9/2011 2011年9月25日 DateTime dateValue=新的DateTime2008,6,11; Console.WriteLine dateValue.DayOfWeek;//显示3 通过对MSDN的搜索,我真的不明白你想要什么。但我会回答我认为我理解的问题 首先,我们对您的代码执行以下操作: 此代码用于C DateTime=DateTi

我想在sql Server 2005或c中获取日期,例如2011年9月的星期日 所以结果应该是 4/9/2011 11/9/2011 18/9/2011 2011年9月25日

DateTime dateValue=新的DateTime2008,6,11; Console.WriteLine dateValue.DayOfWeek;//显示3


通过对MSDN的搜索,我真的不明白你想要什么。但我会回答我认为我理解的问题

首先,我们对您的代码执行以下操作: 此代码用于C

DateTime=DateTime.Now

字符串时间现在

您可以这样做-timenow=time.timeofday.tostring; 巫婆会告诉我们:21:44:15.3487 或者我们可以做-timenow=time.ToLongDateString; 它会告诉我们日期

还有Time.ToShortDateString

只需要你认为你需要的功能

在SQL中,您通常可以执行GetDate

我想我能帮上忙,如果我能帮上忙,我会很高兴的:

首先,我会用那个月的第一天创建一个日期时间

从那里,我会选择DayOfWeek属性,做一些数学减/加运算,找到与您想要的日期匹配的正确日期

从那里,我将继续调用DateTime.AddDays7,并返回这些值,直到month属性结束

我会写一个函数来实现这一点,需要一年、一个月和一周的一天。它将返回一个IEnumerable,并返回每个值

大概是这样的:

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        foreach (var date in DayOfWeek.Monday.OccurrencesInMonth(year: 2011, month: 8))
        {
            Console.WriteLine(date);
        }
    }
}

public static class DayOfWeekExtensions
{
    public static IEnumerable<DateTime> OccurrencesInMonth(this DayOfWeek targetDayOfWeek, int year, int month)
    {
        var firstDayInYear = new DateTime(year, month, 1);
        var currentDay = firstDayInYear.Next(targetDayOfWeek);

        while (currentDay.Month == month)
        {
            yield return currentDay;
            currentDay = currentDay.AddDays(7);
        }
    }
}

public static class DateTimeExtensions
{
    public static DateTime Next(this DateTime current, DayOfWeek dayOfWeek)
    {
        int offsetToNextOccurrence = dayOfWeek - current.DayOfWeek;
        if (offsetToNextOccurrence < 0)
            offsetToNextOccurrence += 7;
        return current.AddDays(offsetToNextOccurrence);
    }
}
DECLARE @Date DATETIME
SELECT @Date = '20110801' 

SELECT  DATEADD(dd, number, @Date) Date
FROM    master..spt_values
WHERE   YEAR(DATEADD(dd, number, @Date)) = YEAR(@Date) AND MONTH(DATEADD(dd, number, @Date)) = MONTH(@Date) AND DATEPART(dw, DATEADD(dd, number, @Date)) = 1
        AND [Type] = 'p'
2011年1月8日12:00:00上午 2011年8月8日12:00:00上午 2011年8月15日12:00:00上午 2011年8月22日12:00:00上午 2011年8月29日12:00:00上午


您可以尝试以下方法:

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        foreach (var date in DayOfWeek.Monday.OccurrencesInMonth(year: 2011, month: 8))
        {
            Console.WriteLine(date);
        }
    }
}

public static class DayOfWeekExtensions
{
    public static IEnumerable<DateTime> OccurrencesInMonth(this DayOfWeek targetDayOfWeek, int year, int month)
    {
        var firstDayInYear = new DateTime(year, month, 1);
        var currentDay = firstDayInYear.Next(targetDayOfWeek);

        while (currentDay.Month == month)
        {
            yield return currentDay;
            currentDay = currentDay.AddDays(7);
        }
    }
}

public static class DateTimeExtensions
{
    public static DateTime Next(this DateTime current, DayOfWeek dayOfWeek)
    {
        int offsetToNextOccurrence = dayOfWeek - current.DayOfWeek;
        if (offsetToNextOccurrence < 0)
            offsetToNextOccurrence += 7;
        return current.AddDays(offsetToNextOccurrence);
    }
}
DECLARE @Date DATETIME
SELECT @Date = '20110801' 

SELECT  DATEADD(dd, number, @Date) Date
FROM    master..spt_values
WHERE   YEAR(DATEADD(dd, number, @Date)) = YEAR(@Date) AND MONTH(DATEADD(dd, number, @Date)) = MONTH(@Date) AND DATEPART(dw, DATEADD(dd, number, @Date)) = 1
        AND [Type] = 'p'

7个问题,不接受,不投票,不尝试自己解决。我在这里看到了一个模式…可能没有,但问题基本上是好的。我想得到的是某一年某个月所有星期日的日期,就像你看到9月有4个星期日,分别是2011年9月4日、2011年9月11日、2011年9月18日、2011年9月25日,所以我想在sql server中得到这4个日期