objectiveC獲取本地時間。

2022-03-12 07:58:51 字數 1728 閱讀 7629

nsdate * date = [nscalendardate date];

nslog(@"%@", date);

日曆在ios裡報錯,,。

nsdateformatter*formatter = [[nsdateformatteralloc] init];

[formatter setdateformat: @"yyyy-mm-dd hh:mm:ss"];

nsstring *curdate = [formatter stringfromdate: self.datepicker.date];

不用去設定datepicker.date的值,取的時候按上面方法取即可。。

nsdate *selected = [self.datepicker date];//這樣取錯誤。

一些時間本地化的**。

nsdate*date1 = [nsdatedate];//格林尼治時間

nsdateformatter*formatter = [[nsdateformatteralloc] init];

[formatter setdateformat:@"yyyy-mm-dd hh:mm:ss zz"];

nsstring*datestring =[formatter stringfromdate:date1];//datestring 為當地時間(東八區時間)的字串

nsdateformatter*formatter1 = [[nsdateformatteralloc] init];

nstimezone*zone = [nstimezonetimezonewithname:@"cst"];

[formatter1 settimezone:zone];

[formatter1 setdateformat:@"yyyy-mm-dd hh:mm:ss zz"];

nsdate*date2 = [formatter1 datefromstring:datestring];//date2 為當地時間(東八區時間)的nsdate例項

nslog(@"date1 is %@",datestring);//列印時用datestring,如果用date2,系統會自動轉成格林尼治時間

Objective C動態獲取例項屬性

本文主要圍繞乙個主題,如何動態獲取例項屬性的值?objective c動行時庫已經有這樣的功能。使用這些方法需要加標頭檔案 import 要用到的方法是 objc property t class copypropertylist class cls,unsigned int outcount 從方...

獲取本地IP

bool socketuti getlocalipaddr string stripaddr if gethostname myname,80 socket error return false if thishost gethostbyname myname return false memset...

獲取本地IP

本機可能會有多個ip,下面這段程式可以獲得本機ip struct hostent h char host 100 int i gethostname host,100 if h gethostbyname host null printf error s n hstrerror h errno re...