Summary of practical methods of objective-c time operation
get the end date according to the month
/** * getmonthenddate * get the end date according to the month * * @param monthbegin month (yyyy-mm-dd HH:mm:ss) * * @return Nsdate/nil */+ (NSDate *) Getmonthenddate: (NSString *) monthbegin{ if (![ Monthbegin isequaltostring:@ ""]) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [Formatter Settimezone:[nstimezone timezonewithabbreviation:@ "Utc+8"]; [Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"]; Nsdatecomponents *currentmonth = [[Nsdatecomponents alloc] init]; Currentmonth.month = 1; NSDate *end = [[Nscalendar Currentcalendar] Datebyaddingcomponents:currentmonth todate:[formatter dateFromString: Monthbegin] options:0]; return end; } else return nil;}
Get week by date
/** * Getweekfordate * Get week by date * * @param nsstring strdate Date (YYYY-MM-DD) * * @return Nsstring/empty */+ (nsstring*) Getweekfordate: (NSString *) strdate{if (![ Strdate isequaltostring:@ ""]) {//time format NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; Get the corresponding time [formatter setdateformat:@ "YYYY-MM-DD"]; NSDate *cdate = [Formatter datefromstring:strdate]; Get time information [formatter setdateformat:@ "Yyyy-mm-dd eeee HH:mm:ss a"]; NSString *locationstring=[formatter Stringfromdate:cdate]; Nsarray *arrdate = [locationstring componentsseparatedbystring:@ ""]; NSString *strweek = [NSString stringwithformat:@ "%@", [Arrdate Objectatindex:1]]; if ([Strweek isequaltostring:@ "Sunday"]) return @ "Weekend"; else if ([Strweek isequaltostring:@ "Monday"]) return @ "Monday"; else if ([Strweek isequaltostring:@ "Tuesday"]) return @ "Tuesday"; else if ([Strweek isequaltostring:@ "Wednesday"]) return @ "Wednesday"; else if ([Strweek isequaltostring:@ "Thursday"]) return @ "Thursday"; else if ([Strweek isequaltostring:@ "Friday"]) return @ "Friday"; else if ([Strweek isequaltostring:@ "Saturday"]) return @ "Saturday"; else return @ ""; }else return nil;}
Gets the UNIX timestamp based on the current time
Get timestamp nsdate *datetime = [NSDate date]; Nstimezone *zone = [Nstimezone timezoneforsecondsfromgmt:8]; Nsinteger interval = [zone secondsfromgmtfordate:datetime]; NSDate *localedate = [datetime datebyaddingtimeinterval:interval]; NSString *unixtime = [NSString stringwithformat:@ "%.f", Floor ([localedate timeIntervalSince1970]);
The following article provides considerable help for this article, expressing gratitude:
Http://book.2cto.com/201304/21439.html
Http://blog.sina.com.cn/s/blog_4e24d9c501010lu2.html
Summary of practical methods of objective-c time operation