Nsdate+category date output to today or yesterday 09:00

Source: Internet
Author: User

1. Create a nsdate category

. h method

-(NSString *) dateamity;

The. M method implements

-(NSString *) dateamity

{

BOOL Isthisyear=false,isthismonth=false,isthisday=false,isyesterday=false,istomorrow=false;

NSString *str = @ "";

NSDate *now = [NSDate Date];

Nsdatecomponents *nowcoms = [[Nscalendar Currentcalendar] components:nscalendarunityear| nscalendarunitmonth| Nscalendarunitday Fromdate:now];

Nsdatecomponents *selfcoms = [[Nscalendar Currentcalendar] components:nscalendarunityear| nscalendarunitmonth| Nscalendarunitday Fromdate:self];

if (nowcoms.year = = selfcoms.year) {

Isthisyear = true;

}

if (Nowcoms.month = = Selfcoms.month && isthisyear) {

Isthismonth = true;

}

if (Nowcoms.day = = Selfcoms.day && isthismonth) {

Isthisday = true;

}else if (nowcoms.day = = selfcoms.day+1 && isthismonth) {

Isyesterday = true;

}else if (nowcoms.day = = Selfcoms.day-1 && isthismonth) {

Istomorrow = true;

}

NSDateFormatter *dateformate = [NSDateFormatter new];

Dateformate.dateformat = @ "Yyyy/mm/dd hh:mm";

NSString *header = @ "";

if (isthisyear) {

Dateformate.dateformat = @ "Mm/dd hh:mm";

if (isthisday) {

Header = @ "Today";

Header = @ "";

}else if (isyesterday) {

Header = @ "Yesterday";

}else if (istomorrow) {

Header = @ "Tomorrow";

}

if (![ Header isequaltostring:@ ""]) {

Dateformate.dateformat = @ "hh:mm";

}

}

str = [NSString stringwithformat:@ "%@%@", Header,[dateformate Stringfromdate:self]];

return str;

}

Nsdate+category date output to today or yesterday 09:00

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.