0

1) How do I find Current Week from Current Date time in C#?

2) Then we have to find given date, Is Exist on corresponding Week dates?

Pls assists to me for solve it?

user1941948
  • 103
  • 1
  • 2
  • 7
  • What do you mean by "Current Week"? The 1st through 52nd week of the year? – Johnny Feb 06 '14 at 10:19
  • possible duplicate of [How do I determine if a date lies between current week dates?](http://stackoverflow.com/questions/21598365/how-do-i-determine-if-a-date-lies-between-current-week-dates) – Liath Feb 06 '14 at 10:29
  • This looks a lot like you've just asked the same question twice? Can you rephrase this one to focus on your 1st question? – Liath Feb 06 '14 at 10:30

3 Answers3

4
int currentWeek = (DateTime.Now.DayOfYear / 7) + 1;
w.b
  • 11,026
  • 5
  • 30
  • 49
0

You have to use a Calendar.

Calendar cal = new Calendar();
int week = cal.GetWeekOfYear(DateTime time, calendarWeekRule rule, DayOfWeek firstDayOfWeek);

Check here: http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear(v=vs.110).aspx

ohlmar
  • 958
  • 8
  • 17
0

To get week number of any given date:

var culture = CultureInfo.CurrentCulture;
int weekNo = culture.Calendar.GetWeekOfYear(
            new DateTime(YOUR_GIVEN_DATE_HERE),
            currentCulture.DateTimeFormat.CalendarWeekRule,
            currentCulture.DateTimeFormat.FirstDayOfWeek);

Just replace YOUR_GIVEN_DATE_HERE with Datetime.Now (for current date) or your any given date. If the weekNos are equal then they are in the same week. That will solve both of your questions.

Lowkey
  • 836
  • 5
  • 12