How can I get the DateTime for the start of the week?

GateKiller

How do I find the start of the week (both Sunday and Monday) knowing just the current time in C#?

Something like:

DateTime.Now.StartWeek(Monday);
Compile This

Use an extension method. They're the answer to everything, you know! ;)

public static class DateTimeExtensions
{
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
        return dt.AddDays(-1 * diff).Date;
    }
}

Which can be used as follows:

DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get the start of the calendar day from a DateTime object?

From Dev

How can I get the previous week in Python?

From Dev

How can I get the previous week in Python?

From Dev

How do I get the start date and end date of the current week?

From Dev

How do I get the Day of Week order to start with Monday

From Dev

How Get Start Day of Week?

From Dev

How can I get the GPS week number given a date in C?

From Dev

how can I get last week' date range in php?

From Dev

How can I get the names of days of week in JodaTime

From Dev

How I can get the date this week between Monday and Sunday AngularJS

From Dev

How can I get one wallpaper for each day of the week?

From Dev

How can I get one wallpaper for each day of the week?

From Dev

How I can get the pre and post dates of the week

From Dev

How can I get 'day of week' for the 'Friday' only?

From Dev

How can I get the GPS week number given a date in C?

From Dev

How can I get a day off next week?

From Dev

How can I get count of rows for every week?

From Dev

DateTime::diff - how to get week number?

From Dev

How can i get last week, current week and last month record from mysql

From Dev

How can i get last week, current week and last month record from mysql

From Dev

Given a Ruby DateTime how can I determine if it represents a day in the current work week?

From Dev

How can I determine that the start of week is Monday or Sunday in different culture in C#

From Dev

How do I get the start date for the current week in SQL Server 2008?

From Dev

How can I test a function that uses DateTime to get the current time?

From Dev

MySQL: how can i get the latest datetime together with the other values?

From Dev

How can I get my YYYYMMDD value to convert to DateTime?

From Dev

Laravel or php I have a week number of a month and a day, how can i get the date of these..?

From Dev

How can I get the start and end address of a struct in C?

From Dev

how can i get a shoucast stream to start playing almost instantly

Related Related

  1. 1

    How can I get the start of the calendar day from a DateTime object?

  2. 2

    How can I get the previous week in Python?

  3. 3

    How can I get the previous week in Python?

  4. 4

    How do I get the start date and end date of the current week?

  5. 5

    How do I get the Day of Week order to start with Monday

  6. 6

    How Get Start Day of Week?

  7. 7

    How can I get the GPS week number given a date in C?

  8. 8

    how can I get last week' date range in php?

  9. 9

    How can I get the names of days of week in JodaTime

  10. 10

    How I can get the date this week between Monday and Sunday AngularJS

  11. 11

    How can I get one wallpaper for each day of the week?

  12. 12

    How can I get one wallpaper for each day of the week?

  13. 13

    How I can get the pre and post dates of the week

  14. 14

    How can I get 'day of week' for the 'Friday' only?

  15. 15

    How can I get the GPS week number given a date in C?

  16. 16

    How can I get a day off next week?

  17. 17

    How can I get count of rows for every week?

  18. 18

    DateTime::diff - how to get week number?

  19. 19

    How can i get last week, current week and last month record from mysql

  20. 20

    How can i get last week, current week and last month record from mysql

  21. 21

    Given a Ruby DateTime how can I determine if it represents a day in the current work week?

  22. 22

    How can I determine that the start of week is Monday or Sunday in different culture in C#

  23. 23

    How do I get the start date for the current week in SQL Server 2008?

  24. 24

    How can I test a function that uses DateTime to get the current time?

  25. 25

    MySQL: how can i get the latest datetime together with the other values?

  26. 26

    How can I get my YYYYMMDD value to convert to DateTime?

  27. 27

    Laravel or php I have a week number of a month and a day, how can i get the date of these..?

  28. 28

    How can I get the start and end address of a struct in C?

  29. 29

    how can i get a shoucast stream to start playing almost instantly

HotTag

Archive