第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在.NET中計算每月的第幾周

在.NET中計算每月的第幾周

ITMISS 2019-11-02 09:46:24
.NET庫是否有一種簡單的方法來返回給定日期的星期數(shù)?例如,輸入Year = 2010, Month = 1, Day = 25,應(yīng)輸出5星期數(shù)。我找到的最接近的是Calendar.GetWeekOfYear,幾乎就在那里。Java具有日期字符串格式“ W”,該字符串每月返回一周,但在.NET中看不到任何等效的形式。
查看完整描述

3 回答

?
飲歌長嘯

TA貢獻(xiàn)1951條經(jīng)驗 獲得超3個贊

沒有內(nèi)置的方法可以執(zhí)行此操作,但是這里有一個擴(kuò)展方法可以為您完成這項工作:


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

    }

}

用法:


DateTime time = new DateTime(2010, 1, 25);

Console.WriteLine(time.GetWeekOfMonth());

輸出:


5

您可以GetWeekOfYear根據(jù)需要進(jìn)行更改。


查看完整回答
反對 回復(fù) 2019-11-02
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗 獲得超5個贊

沒有直接內(nèi)置的方法可以做到這一點(diǎn),但是可以很容易地做到。這是一種擴(kuò)展方法,可用于輕松獲取日期的基于年份的星期數(shù):


public static int GetWeekNumber(this DateTime date)

{

    return GetWeekNumber(date, CultureInfo.CurrentCulture);

}


public static int GetWeekNumber(this DateTime date, CultureInfo culture)

{

    return culture.Calendar.GetWeekOfYear(date,

        culture.DateTimeFormat.CalendarWeekRule,

        culture.DateTimeFormat.FirstDayOfWeek);

}

然后,我們可以使用它來計算基于月份的星期數(shù),就像Jason所示。文化友好版本可能如下所示:


public static int GetWeekNumberOfMonth(this DateTime date)

{

    return GetWeekNumberOfMonth(date, CultureInfo.CurrentCulture);

}


public static int GetWeekNumberOfMonth(this DateTime date, CultureInfo culture)

{

    return date.GetWeekNumber(culture)

         - new DateTime(date.Year, date.Month, 1).GetWeekNumber(culture)

         + 1; // Or skip +1 if you want the first week to be 0.

}


查看完整回答
反對 回復(fù) 2019-11-02
  • 3 回答
  • 0 關(guān)注
  • 799 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號