0

Is there a way to get the number of the week of the year (and for the month too) with Windows Phone (Silverlight 8.1)? I use this code when I create something with C# but I can't find the class GregorianCalendar for Windows Phone.

static class DateTimeExtensions
{
    static GregorianCalendar _gc = new GregorianCalendar();

    public static int GetWeekOfMonth(this DateTime time)
    {
        DateTime first = new DateTime(time.Year, time.Month, 1);
        return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
    }

    static int GetWeekOfYear(this DateTime time)
    {
        return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
    }
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
As As
  • 2,049
  • 4
  • 17
  • 33

1 Answers1

3

The class GregorianCalendar is found in the System.Globalization namespace. From the information on MSDN (check the link), this class is also available for Windows Phone 8.1.

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Otherwise, check the Calendar.GetWeekOfYear() method. This method is also found in the System.Globalization namespace. Method definition:

Returns the week of the year that includes the date in the specified DateTime value.

Community
  • 1
  • 1
Abbas
  • 14,186
  • 6
  • 41
  • 72