Cat Share, must boutique
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243
One: Effect
--because I black Apple, the system time is not allowed to jump time, but the code is not a problem
Two: Implementation code
/** 1. This year 1> today * 1 minutes: just * 1 minutes ~59 min: xx minutes ago * greater than 60 mins: xx hours ago 2> yesterday * Xx:xx 3> Other * XX-XX xx:xx 2. Not this year 1> xxxx-xx-xx xx:xx * *-(NSString *) created_at{nsdateformatter *fmt = [[NSDateFormatter alloc] init];//If it is a real machine debug, convert this European and American time, you need to set localeFmt.locale = [[Nslocale alloc] initwithlocaleidentifier:@"en_US"];//Set date format (the meaning of each number and word inside the declaration string) //E: Day of the week //M: Month //d: What date (the day of the month) //h: 24-hour hour //m: Min //s: SEC //y: YearFmt.dateformat = @"EEE MMM dd HH:mm:ss Z yyyy";//_created_at = @ "Tue Sep +0600 17:06:25"; //Creation date of WeiboNSDate *createdate = [FMT datefromstring:_created_at];//Current timeNSDate *now = [NSDate Date];//Calendar object (easy to compare the gap between two dates)Nscalendar *calendar = [Nscalendar Currentcalendar];The //Nscalendarunit enumeration represents what difference you want to getNscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond;//Calculate the difference between two datesnsdatecomponents *cmps = [Calendar components:unit fromdate:createdate todate:now options:0];if([CreateDate isthisyear]) {//This year if([CreateDate Isyesterday]) {//YesterdayFmt.dateformat = @"Yesterday hh:mm";return[FMT Stringfromdate:createdate]; }Else if([CreateDate IsToday]) {//Today if(Cmps.hour >=1) {return[NSString stringwithformat:@"%d hours ago", Cmps.hour]; }Else if(Cmps.minute >=1) {return[NSString stringwithformat:@"%d minutes ago", Cmps.minute]; }Else{return@"just"; } }Else{//Other days of the yearFmt.dateformat = @"Mm-dd hh:mm";return[FMT Stringfromdate:createdate]; } }Else{//Not this yearFmt.dateformat = @"Yyyy-mm-dd hh:mm";return[FMT Stringfromdate:createdate]; }}
Time classification to make judgments
Here is the time classification to make a judgment whether it is today, whether it is tomorrow or something, in short, this is very powerful
////NSDATE+EXTENSION.M//Black horse Weibo 2////Created by Apple on 14-10-18.//Copyright (c) 2014 Heima. All rights reserved.//#Import "Nsdate+extension.h"@implementationNSDate (Extension)/** * To determine if a time is this year * /-(BOOL) isthisyear{Nscalendar *calendar = [Nscalendar Currentcalendar];//Get a time of year month day secondsnsdatecomponents *datecmps = [Calendar components:nscalendarunityear fromdate:self]; nsdatecomponents *nowcmps = [Calendar components:nscalendarunityear fromdate:[nsdate Date]];returnDatecmps.year = = Nowcmps.year;}/** * Determine if a time is yesterday * /-(BOOL) isyesterday{nsdate *now = [NSDate Date];//Date = = 2014-04-30 10:05:28--2014-04-30 00:00:00 //now = = 2014-05-01 09:22:10--2014-05-01 00:00:00NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; Fmt.dateformat = @"Yyyy-mm-dd";//2014-04-30NSString *datestr = [FMT stringfromdate:self];//2014-10-18NSString *nowstr = [FMT Stringfromdate:now];//2014-10-30 00:00:00NSDate *date = [FMT datefromstring:datestr];//2014-10-18 00:00:00now = [FMT datefromstring:nowstr]; Nscalendar *calendar = [Nscalendar Currentcalendar]; Nscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday; nsdatecomponents *cmps = [Calendar components:unit fromdate:date todate:now options:0];returnCmps.year = =0&& Cmps.month = =0&& Cmps.day = =1;}/** * To determine if a time is today * /-(BOOL) istoday{nsdate *now = [NSDate Date]; NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; Fmt.dateformat = @"Yyyy-mm-dd"; NSString *datestr = [FMT stringfromdate:self]; NSString *nowstr = [FMT Stringfromdate:now];return[Datestr isequaltostring:nowstr];}@end
Third: Supplement
Week and Month English
January January abbreviation Jan.
February February.
March March April.
April abbreviated APR April.
May May
June June abbreviated June.
July July abbreviation Jul.
August August.
September September abbreviated Sep.
October October abbreviated OCT.
November November abbreviated to Nov.
December December.
Monday: Mon.=monday
Tuesday: Tue.=tuesday
Wednesday: Wed.=wednesday
Thursday: Thu.=thursday
Friday: Fri.=friday
Saturday: Sat.=saturday
Sunday: Sun.=sunday
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cat Learning iOS Weibo project Combat (9) Weibo model time related important operation, judging just yesterday, this year and so on