一周的第一个星期一是星期几?(复制)

时间:2022-10-21 10:48:27

This question already has an answer here:

这个问题已经有了答案:

I was wondering if you guys know how to get the date of currents week's monday based on todays date?

我想知道你们是否知道以今天的日期为基础的电流星期一的日期?

i.e 2009-11-03 passed in and 2009-11-02 gets returned back

我。e 2009-11-03通过,2009-11-02被退回

/M

/ M

7 个解决方案

#1


128  

This is what i use (probably not internationalised):

这是我使用的(可能不是国际化的):

DateTime input = //...
int delta = DayOfWeek.Monday - input.DayOfWeek;
DateTime monday = input.AddDays(delta);

#2


54  

The Pondium answer can search Forward in some case. If you want only Backward search I think it should be:

在某些情况下,浮想联翩的答案是可以探寻的。如果你只想要反向搜索,我认为应该是:

DateTime input = //...
int delta = DayOfWeek.Monday - input.DayOfWeek;
if(delta > 0)
    delta -= 7;
DateTime monday = input.AddDays(delta);

#3


6  

Something like this would work

像这样的东西可以用

DateTime dt = DateTime.Now;
while(dt.DayOfWeek != DayOfWeek.Monday) dt = dt.AddDays(-1); 

I'm sure there is a nicer way tho :)

我相信还有更好的办法

#4


4  

public static class DateTimeExtension
{
    public static DateTime GetFirstDayOfWeek(this DateTime date)
    {
        var firstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

        while (date.DayOfWeek != firstDayOfWeek)
        {
            date = date.AddDays(-1);
        }

        return date;
    }
}

International here. I think as extension it can be more useful.

国际。我认为作为延伸它可以更有用。

#5


3  

What about:

是什么:

CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek

Why don't use native solution?

为什么不使用本机解决方案呢?

#6


1  

var now = System.DateTime.Now;

var result = now.AddDays(-((now.DayOfWeek - System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7)).Date;

Probably will return you with Monday. Unless you are using a culture where Monday is not the first day of the week.

很可能星期一还你。除非你使用的文化是星期一不是一周的第一天。

#7


0  

Try this:

试试这个:

public DateTime FirstDayOfWeek(DateTime date)
{
    var candidateDate=date;
    while(candidateDate.DayOfWeek!=DayOfWeek.Monday) {
        candidateDate=candidateDate.AddDays(-1);
    }
    return candidateDate;
}

EDIT for completeness: overload for today's date:

编辑完整性:今天日期的过载:

public DateTime FirstDayOfCurrentWeek()
{
    return FirstDayOfWeek(DateTime.Today);
}

#1


128  

This is what i use (probably not internationalised):

这是我使用的(可能不是国际化的):

DateTime input = //...
int delta = DayOfWeek.Monday - input.DayOfWeek;
DateTime monday = input.AddDays(delta);

#2


54  

The Pondium answer can search Forward in some case. If you want only Backward search I think it should be:

在某些情况下,浮想联翩的答案是可以探寻的。如果你只想要反向搜索,我认为应该是:

DateTime input = //...
int delta = DayOfWeek.Monday - input.DayOfWeek;
if(delta > 0)
    delta -= 7;
DateTime monday = input.AddDays(delta);

#3


6  

Something like this would work

像这样的东西可以用

DateTime dt = DateTime.Now;
while(dt.DayOfWeek != DayOfWeek.Monday) dt = dt.AddDays(-1); 

I'm sure there is a nicer way tho :)

我相信还有更好的办法

#4


4  

public static class DateTimeExtension
{
    public static DateTime GetFirstDayOfWeek(this DateTime date)
    {
        var firstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

        while (date.DayOfWeek != firstDayOfWeek)
        {
            date = date.AddDays(-1);
        }

        return date;
    }
}

International here. I think as extension it can be more useful.

国际。我认为作为延伸它可以更有用。

#5


3  

What about:

是什么:

CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek

Why don't use native solution?

为什么不使用本机解决方案呢?

#6


1  

var now = System.DateTime.Now;

var result = now.AddDays(-((now.DayOfWeek - System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7)).Date;

Probably will return you with Monday. Unless you are using a culture where Monday is not the first day of the week.

很可能星期一还你。除非你使用的文化是星期一不是一周的第一天。

#7


0  

Try this:

试试这个:

public DateTime FirstDayOfWeek(DateTime date)
{
    var candidateDate=date;
    while(candidateDate.DayOfWeek!=DayOfWeek.Monday) {
        candidateDate=candidateDate.AddDays(-1);
    }
    return candidateDate;
}

EDIT for completeness: overload for today's date:

编辑完整性:今天日期的过载:

public DateTime FirstDayOfCurrentWeek()
{
    return FirstDayOfWeek(DateTime.Today);
}