iOS 獲取當前時間以及計算年齡(時間差)

2021-07-16 20:06:58 字數 3188 閱讀 7669

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

第一種僅僅得出年份差的年齡

//計算年齡

nsstring *birth = @"1993-10-30";

nsdateformatter *dateformatter = [[nsdateformatter alloc]init];

[dateformatter setdateformat:@"yyyy-mm-dd"];

//生日

nsdate *birthday = [dateformatter datefromstring:birth];

//當前時間

nsstring *currentdatestr = [dateformatter stringfromdate:[nsdate date]];

nsdate *currentdate = [dateformatter datefromstring:currentdatestr];

nslog(@"currentdate %@ birthday %@",currentdatestr,birth);

nstimeinterval time=[currentdate timeintervalsincedate:birthday];

int age = ((int)time)/(3600*24*365);

nslog(@"year %d",age);

另外一種得出詳細到天的年齡

nscalendar *calendar = [nscalendar currentcalendar];//定義乙個nscalendar物件

nsdate *nowdate = [nsdate date];

nsstring *birth = @"1900-10-30";

nsdateformatter *dateformatter = [[nsdateformatter alloc]init];

[dateformatter setdateformat:@"yyyy-mm-dd"];

//生日

nsdate *birthday = [dateformatter datefromstring:birth];

//用來得到詳細的時差

unsigned int unitflags = nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit | nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit;

nsdatecomponents *date = [calendar components:unitflags fromdate:birthday todate:nowdate options:0];

if([date year] >0)

else if([date month] >0)

else if([date day]>0)

else

字串、日期、int轉換

此項參考:

1、字串轉換為日期

nsdateformatter* dateformat = [[nsdateformatter alloc] init];//例項化乙個nsdateformatter物件

[dateformat setdateformat:@"yyyy-mm-dd hh:mm:ss"];//設定時間格式,這裡能夠設定成自己須要的格式

nsdate *date =[dateformat datefromstring:@"1980-01-01 00:00:01"];

2、日期轉換為字串

nsdateformatter* dateformat = [[nsdateformatter alloc] init];//例項化乙個nsdateformatter物件

[dateformat setdateformat:@"yyyy-mm-dd hh:mm:ss"];//設定時間格式,這裡能夠設定成自己須要的格式

nsstring *currentdatestr = [dateformat stringfromdate:[nsdate date]];

3、字串轉int

convert nsstring to int

nsstring *anumberstring = @"123";

int i = [anumberstring intvalue];

4、int轉字串

convert int to nsstring

int anumber = 123;

nsstring *astring = [nsstring stringwithformat:@"%d", anumber];

iOS 獲取當前時間以及計算年齡(時間差)

nsdate now nsdate date nslog now date is now nscalendar calendar nscalendar currentcalendar nsuinteger unitflags nsyearcalendarunit nsmonthcalendaruni...

iOS 獲取當前時間和當前時間戳

獲取當前的時間 nsstring getcurrenttimes nsdateformatter formatter nsdateformatter alloc init 設定你想要的格式,hh與hh的區別 分別表示12小時制,24小時制 formatter setdateformat yyyy m...

iOS 獲取當前時間和當前時間戳

獲取當前的時間 nsstring getcurrenttimes nsdateformatter formatter nsdateformatter alloc init 設定你想要的格式,hh與hh的區別 分別表示12小時制,24小時制 formatter setdateformat yyyy m...