0

I'm working in ASP using MVC 4, and i try to know the number of week in month. For example, the user inserts 05/02/2014, and i in controller needs to know the number of week from this date. For example: The user insert 04/02/2014 and i gets result 6

Edit: Using format dd/mm/yyyy

CesarMiguel
  • 3,756
  • 4
  • 24
  • 34
  • How does 04/02/2014 result in 6? If 04/02 represents mm/dd there are five weeks in April. If it represents dd/mm there are still five weeks in February. – Craig W. Feb 04 '14 at 15:52
  • `var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar; cal.GetWeekOfYear(DateTime.Now,System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);` Try that. – ramiramilu Feb 04 '14 at 15:53
  • Sorry, i'm using format dd/mm – CesarMiguel Feb 04 '14 at 15:53
  • See [this](http://stackoverflow.com/a/5893267/304683) – EdSF Feb 04 '14 at 15:55
  • Ah, now I understand. You're trying to get the number of that week in the year. In other words, you would expect a number between 1 and 52. I misread and thought you wanted to know how many weeks were in the month represented by the date. – Craig W. Feb 04 '14 at 15:59

1 Answers1

0

This should solve your problem -

UPDATED With string DateTime conversion

DateTime dt = DateTime.ParseExact("28/02/2014","dd/MM/yyyy", CultureInfo.InvariantCulture);
var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
cal.GetWeekOfYear(dt, System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
ramiramilu
  • 17,044
  • 6
  • 49
  • 66