Compact format for IOS date display

Source: Internet
Author: User
Tags compact dateformat time and seconds

First of all, I want to explain the meaning of the title, the concise format of the date of the presentation, the reason is because to let people at a glance, do not need to think about when.

In detail is the time format we see in the circle of friends.

For example: Just a few minutes ago-a few hours ago and so on.

What's going on here today is a simple category.

Convenient and practical.

and a simple practical application.

Let's take a look at what the category looks like.

. h

#import <Foundation/Foundation.h>@interface  nsdate (cxextension)-( Nsdatecomponents *) Datefrom: (NSDate *) from; -(BOOL) isthisyear; -(BOOL) isToday; -(BOOL) isyesterday; @end

. m

#import "Nsdate+cxextension.h"@implementationnsdate (cxextension)-(Nsdatecomponents *) Datefrom: (NSDate *) from{//CalendarNscalendar *calendar =[Nscalendar Currentcalendar]; Nscalendarunit Unit= nscalendarunityear| nscalendarunitmonth| nscalendarunitday| nscalendarunithour| nscalendarunitminute|Nscalendarunitsecond; return[Calendar Components:unit fromdate:from todate:self options:0 ];}//Judging whether this is the year-(BOOL) isthisyear{Nscalendar* Calendar =[Nscalendar Currentcalendar]; Nsinteger Nowyear=[Calendar component:nscalendarunityear fromdate:[nsdate Date]]; Nsinteger Selfyear=[Calendar Component:nscalendarunityear fromdate:self]; returnSelfyear = =nowyear; }//is the interpretation today-(BOOL) istoday{NSDateFormatter* Formatter =[[NSDateFormatter alloc]init]; Formatter.dateformat=@"YYYY-MM-DD"; NSString* Nowdate =[Formatter stringfromdate:[nsdate Date]]; NSString* Selfdate =[Formatter stringfromdate:self]; return[Nowdate isequaltostring:selfdate]; }//to judge whether it was yesterday-(BOOL) isyesterday{NSDateFormatter* Formatter =[[NSDateFormatter alloc]init]; Formatter.dateformat=@"YYYY-MM-DD"; NSDate* Nowdate =[Formatter datefromstring:[formatter stringfromdate:[nsdate Date]]; NSDate* Selfdate =[Formatter datefromstring:[formatter stringfromdate:self]; Nscalendar* Calendar =[Nscalendar Currentcalendar]; Nsdatecomponents* unit = [Calendar components:nscalendarunityear| nscalendarunitmonth| Nscalendarunitday fromdate:selfdate todate:nowdate Options:0]; returnUnit.year = =0&& Unit.month = =0&& Unit.day = =1; }@end

The following strength shows the effect:

Show full time format when time is last year

When the time for this year is divided into the following situations

When the time for today is to judge the specific time

Produced a few hours ago

A few minutes ago.

Just the display format

When the time for yesterday is shown yesterday in time and seconds

When time is neither today nor yesterday

Show all time formats except years

-(NSString *) create_time{NSDateFormatter* Formatter =[[NSDateFormatter alloc]init]; Formatter.dateformat=@"YYYY-MM-DD HH:mm:ss"; NSDate* creat =[Formatter datefromstring:_create_time]; if(creat.isthisyear) {if(creat.istoday) {nsdatecomponents* unit =[creat datefrom:creat]; if(Unit.hour >1) {                return[NSString stringWithFormat:@"%zd hours ago", Unit.hour]; }Else if(Unit.minute >1){                return[NSString stringWithFormat:@"%zd minutes ago", Unit.minute]; }Else{                return @"just"; }        }Else if(creat.isyesterday) {Formatter.dateformat=@"Yesterday HH:mm:ss"; return[Formatter stringfromdate:creat]; }Else{Formatter.dateformat=@"MM-DD HH:mm:ss"; return[Formatter stringfromdate:creat]; }    }    Else{        return[Formatter stringfromdate:creat]; }}

Compact format for IOS date display

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.