1

我在VS 2012中爲使用silverlight的windows phone編寫了一個程序,現在我試圖將我的程序導入到VS 2015 Universal應用程序中。在我的計劃,我需要得到給定日期的週數,爲他我寫的跟隨着功能WinRT中的WeekNumber

public int WeekNumber(DateTime date) 
{ 
    GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized); 
    return cal.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday); 
} 

但在WinRT中,沒有GregorianCalendar的,我也嘗試創建壓延這樣:

Windows.Globalization.Calendar cal = new Windows.Globalization.Calendar(); 

然後嘗試獲得星期年,但沒有這樣的方法。

任何想法如何獲得winRT中給定日期的週數。

非常感謝。

+0

不確定是否有內置任何東西,但您始終可以自己實現算法。 http://en.wikipedia.org/wiki/ISO_week_date#Calculating_the_week_number_of_a_given_date – CoderDennis

+0

你確定這是你想要遵循的週數規則嗎?我只問,因爲有很多不同的選擇,而且很容易出錯。 –

+0

你正在用C#編寫,你沒有*使用WinRT api。您仍然有.NET框架可以幫助您,CultureInfo.CurrentCulture隨時可用。 –

回答

1

我發現,使用Windows.Globalization.Calendar時,您沒有GetWeekOfYear方法,但是如果您使用System.Globalization.Calendar,那麼您會得到GetWeekOfYear方法的工作方式。基於此,下面的代碼根據需要工作。

public int WeekNumber(DateTime date) 
{   
    DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo; 
    System.Globalization.Calendar cal = dfi.Calendar; 
    return cal.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday); 
}