獲取當天是當年 當月的第幾周

2021-09-24 20:22:31 字數 1227 閱讀 7905

引用命名空間:

using system.globalization;

示例**:

datetime date =datetime.now;

//獲取指定時間是當年的第幾周

gregoriancalendar gc = new

gregoriancalendar();

int weekofyear =gc.getweekofyear(date, calendarweekrule.firstday, dayofweek.monday);

console.writeline(

"是當年的第周

", date.tostring("

yyyy年mm月dd日

"), weekofyear);

console.readkey();

//獲取是當月的第幾周

int dayinmonth =date.day;

//本月第一天

datetime firstday = date.adddays(1 -date.day);

//本月第一天是週幾

int weekday = (int)firstday.dayofweek == 0 ? 7 : (int

)firstday.dayofweek;

//本月第一周有幾天

int firstweekendday = 7 - (weekday - 1

);

//當前日期和第一周之差

int diffday = dayinmonth -firstweekendday;

diffday = diffday > 0 ? diffday : 1

;

//當前是第幾周,如果整除7就減一天

int weeknuminmonth = ((diffday % 7) == 0

? (diffday / 7 - 1

) : (diffday / 7)) + 1 + (dayinmonth > firstweekendday ? 1 : 0

); console.writeline(

"是當月的第周

", date.tostring("

yyyy年mm月dd日

"), weeknuminmonth);

console.readkey();

獲取當天是當年 當月的第幾周

引用命名空間 using system.globalization 示例 datetime date datetime.now 獲取指定時間是當年的第幾周 gregoriancalendar gc new gregoriancalendar int weekofyear gc.getweekofye...

Oracle獲取當天,當月,當年的資料

between to date to char sysdate,yyyy mm dd 00 00 01 yyyy mm dd hh24 mi ss and to date to char sysdate,yyyy mm dd 23 59 59 yyyy mm dd hh24 mi ss oracle...

JAVA獲取日期屬於當年第幾周

string today 2017 11 11 dateformat format new dateformat yyyy mm dd date date format.parse today calendar calendar calendar.getinstance calendar.setfi...