user1941948
user1941948

Reputation: 103

Find the Current Week from Datetime.Now and How to find given Date between current week dates?

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?

Upvotes: 0

Views: 1404

Answers (3)

Lowkey
Lowkey

Reputation: 836

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.

Upvotes: 0

ohlmar
ohlmar

Reputation: 956

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

Upvotes: 0

w.b
w.b

Reputation: 11228

int currentWeek = (DateTime.Now.DayOfYear / 7) + 1;

Upvotes: 4

Related Questions