NSDate 時間比較

2021-06-20 05:23:34 字數 1181 閱讀 4349

由 nsdate 轉換為 nsstring:

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

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

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

nslog(@"%@", strdate);

[dateformatter release];

結果:

2010-08-04 16:01:03

由 nsstring 轉換為 nsdate:

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

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

nsdate *date = [dateformatter datefromstring:@"2010-08-04 16:01:03"];

nslog(@"%@", date);

[dateformatter release];

結果:

2010-08-04 16:01:03 +0800

日期之間比較可用以下方法

- (bool)isequaltodate:(nsdate *)otherdate;

與otherdate比較,相同返回yes

- (nsdate *)earlierdate:(nsdate *)anotherdate;

與anotherdate比較,返回較早的那個日期

- (nsdate *)laterdate:(nsdate *)anotherdate;

與anotherdate比較,返回較晚的那個日期

- (nscomparisonresult)compare:(nsdate *)other;

該方法用於排序時呼叫:

. 當例項儲存的日期值與anotherdate相同時返回nsorderedsame

. 當例項儲存的日期值晚於anotherdate時返回nsordereddescending

. 當例項儲存的日期值早於anotherdate時返回nsorderedascending

NSDate 時間比較

由 nsdate 轉換為 nsstring nsdateformatter dateformatter nsdateformatter alloc init dateformatter setdateformat yyyy mm dd hh mm ss nsstring strdate datefo...

mysql時間比較

時間比較 當前時間是否在某個時間段之內 是否在create time 5天之內 select from message detail where unix timestamp now between unix timestamp create time and unix timestamp crea...

vue時間比較

所有時間比較的,用時間戳是最簡單的方式 比較時間,一律轉成時間戳 ios不認識 1970 01 01 let workstarttime new date 1970 01 01 this.form.workstarttime 00 gettime 上班時間 workendtime new date ...