使用Windows Phone的月份/年份的周

本文关键字:Windows Phone 使用 | 更新日期: 2023-09-27 18:25:44

是否有办法使用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);
    }
}

使用Windows Phone的月份/年份的周

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

.NET for Windows Phone应用

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

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

返回一年中的星期,该星期包括指定DateTime值中的日期。