开发者

How to get the date of a day in a month in certain year? [closed]

It's difficult to tell what is being asked here. This qu开发者_StackOverflow中文版estion is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I want to get the dates of , for example the dates of Sunday during September 2011 in sql sever 2005, or in c# so the result should be 4/9/2011 11/9/2011 18/9/2011 25/9/2011


First I'd create a DateTime using the first day of that month.

From there, I'd pick the DayOfWeek property, do some math (subtract/add) to find the right date that week that matched the date you want.

From there, I'd keep calling DateTime.AddDays(7), and return those values until the month property rolled over.

I'd write a function that did this, that takes a year, a month, and a DayOfWeek. It would return an IEnumerable<DateTime>, and yield return each value.

Something like this:

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);
    }
}

8/1/2011 12:00:00 AM
8/8/2011 12:00:00 AM
8/15/2011 12:00:00 AM
8/22/2011 12:00:00 AM
8/29/2011 12:00:00 AM


.NET C#

DateTime dateValue = new DateTime(2008, 6, 11); Console.WriteLine((int) dateValue.DayOfWeek); // Displays 3

From a search on MSDN


I didnt really understand what you want. but i will answer as i think i understand.

Frist of all we do this on your code: This code if for C#

DateTime time = DateTime.Now;

string timenow;

you can do this - timenow = time.timeofday.tostring(); witch wil give us: 21:44:15.3487 or we can do - timenow = time.ToLongDateString(); it will give us the date.

There is also Time.ToShortDateString();.

Just take the function that you think you need.

In SQL you can alaways do "GetDate()".

I think i help, happy if i did :)


You can try something like this:

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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜