ios根據日期獲取年月日分秒及常用操作 問題

2021-07-04 16:42:30 字數 1679 閱讀 7681

//獲取當前時間

nsdate *now = [nsdate date];

nslog(@"now date is: %@", now);

nscalendar *calendar = [nscalendar currentcalendar];

nsuinteger unitflags = nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit | nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit;

nsdatecomponents *datecomponent = [calendar components:unitflags fromdate:now];

int year = [datecomponent year];

int month = [datecomponent month];

int day = [datecomponent day];

int hour = [datecomponent hour];

int minute = [datecomponent minute];

int second = [datecomponent second];

nslog(@"year is: %d", year);

nslog(@"month is: %d", month);

nslog(@"day is: %d", day);

nslog(@"hour is: %d", hour);

nslog(@"minute is: %d", minute);

nslog(@"second is: %d", second);

計算n小時後的日期

/**

* 根據開始小時,分鐘和時長計算結束日期

* * @param starthour 開始日期對應的小時(以當前日期為準)

* @param startminute 開始日期對應的分鐘(以當前日期為準)

* @param endhour 結束的時長 (時長)

* @param endminute 結束的分鐘 (時長)

* * @return 結束時期

*/- (nsdate *) calculateenddate:(nsinteger)starthour startminute:(nsinteger)startminute endhour:(nsinteger)endhour endminute:(nsinteger)endminute

nsdate *date = [nsdate date];

nstimezone *zone = [nstimezone systemtimezone];

nsinteger interval = [zone secondsfromgmtfordate: date];

nsdate *localedate = [date datebyaddingtimeinterval: interval];

nslog(@"enddate=%@",localedate);

ios nscalendaridentifiergregorian日期相差8小時:

獲取年月日時分秒

calendar ca calendar.getinstance int year ca.get calendar.year 獲取年份 2016 system.out.println year int month ca.get calendar.month 獲取月份 10 1 system.out....

獲取年月日

需求 獲取當前日期的前乙個月份 當月有 31 天時,js 日期物件 setmonth 問題 當前日期如果不是 31 號,是沒問題的,是 31 號就會有問題 比如今天是 2018 09 30 號,前乙個月應該是 2018 08 30 let now new date new date 2018 09 ...

C 根據年 月 日獲取星期幾

1 根據日期,獲得星期幾 年 月 日 星期幾,1代表星期一 7代表星期日 public static int getweekday int y,int m,int d c 的功能很強大,卻沒有直接提供面向漢字文化的開發傾向 比如我現在要說的獲取當前的星期我提供兩種方法 datetime.now.da...