使用Windows Phone的每月/每周的周

时间:2014-10-07 10:23:17

标签: c# datetime windows-phone-8.1

有没有办法通过Windows Phone(Silverlight 8.1)获取一年中的一周(以及一个月)?我在使用C#创建内容时使用此代码但我找不到Windows Phone的类GregorianCalendar

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);
    }
}

1 个答案:

答案 0 :(得分:3)

GregorianCalendar位于System.Globalization命名空间中。从MSDN上的信息(请查看链接),此类也适用于Windows Phone 8.1。

  

适用于Windows Phone应用的.NET

     

支持:Windows Phone 8.1,Windows Phone Silverlight 8.1,Windows Phone Silverlight 8

否则,请检查Calendar.GetWeekOfYear()方法。此方法也可在System.Globalization命名空间中找到。方法定义:

  

返回包含指定DateTime值中日期的一年中的一周。

相关问题