获取本周第一个星期一的日期?

发布于 2024-08-09 14:01:55 字数 88 浏览 4 评论 0原文

我想知道你们是否知道如何根据今天的日期获取当前周星期一的日期?

即 2009-11-03 传入并返回 2009-11-02

/M

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

/M

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

捎一片雪花 2024-08-16 14:01:55

这就是我使用的(可能没有国际化):

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

This is what i use (probably not internationalised):

DateTime input = //...
int delta = DayOfWeek.Monday - input.DayOfWeek;
DateTime monday = input.AddDays(delta);
葬花如无物 2024-08-16 14:01:55

Pondium 答案在某些情况下可以向前搜索。如果你只想向后搜索,我认为应该是:

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

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);
偏爱你一生 2024-08-16 14:01:55

像这样的东西会起作用,

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

我确信有更好的方法:)

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 :)

小矜持 2024-08-16 14:01:55
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;
    }
}

这里国际化。我认为作为扩展它会更有用。

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.

残花月 2024-08-16 14:01:55

怎么样:

CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek

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

What about:

CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek

Why don't use native solution?

残疾 2024-08-16 14:01:55
var now = System.DateTime.Now;

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

可能会在周一回来给你。除非您使用的文化中星期一不是一周的第一天。

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.

听闻余生 2024-08-16 14:01:55

试试这个:

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

编辑以确保完整性:今天的日期超载:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文