A tool class for displaying dates

Source: Internet
Author: User

A tool class for displaying dates

. H file

#import <Foundation/Foundation.h>@interface TimeUtil : NSObject+ (NSString*)getTimeStr1:(long long)time;+(NSString*) getTimeStrStyle1:(long long)time;+ (NSString*)getTimeStr1Short:(long long)time;+(NSString*) getTimeStrStyle2:(long long)time;+(int)dayCountForMonth:(int)month andYear:(int)year;+(BOOL)isLeapYear:(int)year;@end

. M file

# Import "timeutil. H "@ implementation timeutil + (nsstring *) gettimestr :( long) createdat {// calculate distance time string // nsstring * timestamp; time_t now; time (& now ); int distance = (INT) difftime (now, createdat); If (distance <0) distance = 0; If (distance <60) {timestamp = [nsstring stringwithformat: @ "% d % s", distance, (distance = 1 )? "Second ago": "seconds ago"];} else if (distance <60*60) {distance = distance/60; timestamp = [nsstring stringwithformat: @ "% d % s", distance, (distance = 1 )? "Minute ago": "Minutes Ago"];} else if (distance <60*60*24) {distance = distance/60/60; timestamp = [nsstring stringwithformat: @ "% d % s", distance, (distance = 1 )? "Hour ago": "Hours Ago"];} else if (distance <60*60*24*7) {distance = distance/60/60/24; timestamp = [nsstring stringwithformat: @ "% d % s", distance, (distance = 1 )? "Day Ago": "Days Ago"];} else if (distance <60*60*24*7*4) {distance = distance/60/60/24/7; timestamp = [nsstring stringwithformat: @ "% d % s", distance, (distance = 1 )? "Week Ago": "weeks ago"];} else {static nsdateformatter * dateformatter = nil; If (dateformatter = nil) {dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdatestyle: nsdateformatterregular style]; [dateformatter settimestyle: nsdateformatterregular style];} nsdate * Date = [nsdate sequence: createdat]; timestamp = [dateformatter stringfromdate: date];} return timest Amp;} + (nsstring *) gettimestr1 :( long) time {nsdate * Date = [nsdate datewithtimeintervalsince1970: Time]; nscalendar * Calendar = [[nscalendar alloc] callback: nsgregoriancalendar]; nsinteger unitflags = nsmonthcalendarunit | nsdaycalendarunit | nsyearcalendarunit | nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit; nsdatecomponents * Component = [calendar components: unitflags F Romdate: date]; nsstring * string = [nsstring stringwithformat: @ "% 04d-% 02d-% 02d % 02d: % 02d", [component year], [component month], [component Day], [component hour], [component minute]; return string ;}+ (nsstring *) gettimestr1short :( long) time {nsdate * Date = [nsdate datewithtimeintervalsince1970: time]; nscalendar * Calendar = [[nscalendar alloc] initwithcalendaridentifier: nsgregoriancalendar]; nsinteger UN Itflags = calendar | nsdaycalendarunit | calendar | nshourcalendarunit | calendar | nssecondcalendarunit; nsdatecomponents * Component = [calendar components: unitflags fromdate: date]; nsstring * string = [nsstring stringwithformat: @ "% 04d-% 02d-% 02d", [component year], [component month], [component Day]; return string ;}+ (nsstring *) getmdstr :( long) time {nsdate * Date = [nsdate d Expiration: Time]; nscalendar * Calendar = [[nscalendar alloc] calendar: nsgregoriancalendar]; nsinteger unitflags = calendar | nsdaycalendarunit | nsyearcalendarunit | nshourcalendarunit | calendar | nssecondcalendarunit; nsdatecomponents * Component = [calendar components: unitflags fromdate: date]; nsstring * string = [nsstring stringwithformat: @ "% d month % d day ", [Component month], [component Day]; return string ;}+ (nsdatecomponents *) getcomponent :( long) time {nsdate * Date = [nsdate datewithtimeintervalsince1970: Time]; nscalendar * Calendar = [[nscalendar alloc] calendar: nsgregoriancalendar]; nsinteger unitflags = calendar | nsdaycalendarunit | nsyearcalendarunit | nshourcalendarunit | calendar | nssecondcalendarunit; nsdate Components * Component = [calendar components: unitflags fromdate: date]; return component;} + (nsstring *) gettimestrstyle1 :( long) time {nsdate * Date = [nsdate datewithtimeintervalsince1970: time]; nscalendar * Calendar = [[nscalendar alloc] calendar: nsgregoriancalendar]; nsinteger unitflags = nsmonthcalendarunit | nsdaycalendarunit | nsyearcalendarunit | nshourcalendarunit | Interval Unit | nssecondcalendarunit | nsweekcalendarunit | nsweekdaycalendarunit; nsdatecomponents * Component = [calendar components: unitflags fromdate: date]; int year = [component year]; int month = [component month]; int day = [component Day]; int hour = [component hour]; int minute = [component minute]; nsdate * today = [nsdate date]; Component = [calendar components: unitflags fromdate: Today]; int t_year = [component year]; Nsstring * string = nil; long now = [Today timeintervalsince1970]; long distance = now-time; If (distance <60) [email protected] "just now "; else if (distance <60*60) string = [nsstring stringwithformat: @ "% LLD Minutes Ago", distance/60]; else if (distance <60*60*24) string = [nsstring stringwithformat: @ "% LLD Hours Ago", distance/60/60]; else if (distance <60*60*24*7) string = [nsstring stringwithformat: @ "% LLD days ago", distance/60/60/24]; e LSE if (year = t_year) string = [nsstring stringwithformat: @ "% 02d-% 02d % d: % 02d", month, day, hour, minute]; else string = [nsstring stringwithformat: @ "% d-% d", year, month, day]; return string ;}+ (nsstring *) gettimestrstyle2 :( long) time {nsdate * Date = [nsdate datewithtimeintervalsince1970: Time]; nscalendar * Calendar = [[nscalendar alloc] initwithcalendaridentifier: nsgregoriancalendar]; nsinteger unitflags = Calendar | nsdaycalendarunit | calendar | nshourcalendarunit | calendar | nssecondcalendarunit | nsweekcalendarunit | calendar; nsdatecomponents * Component = [calendar components: unitflags fromdate: date]; int year = [component year]; int month = [component month]; int day = [component Day]; int hour = [component hour]; int minute = [component minute]; int week = [component W Eek]; int weekday = [component weekday]; nsdate * today = [nsdate date]; Component = [calendar components: unitflags fromdate: Today]; int t_year = [component year]; int t_month = [component month]; int t_day = [component Day]; int t_week = [component week]; nsstring * string = nil; if (year = t_year & month = t_month & day = t_day) {If (hour <6 & hour> = 0) string = [nsstring stringwithformat: @ "% d: % 02d", hour, minute]; else if (Hour> = 6 & hour <12) string = [nsstring stringwithformat: @ "% d: % 02d", hour, minute]; else if (hour> = 12 & hour <18) string = [nsstring stringwithformat: @ "% d: % 02d", hour, minute]; else string = [nsstring stringwithformat: @ "% d: % 02d", hour, minute];} else if (year = t_year & week = t_week) {nsstring * daystr = nil; Switch (weekday) {Case 1: [email protected] ""; break; Case 2: [email protected] "1"; break; case 3: [Email protected] "2"; break; Case 4: [email protected] "3"; break; Case 5: [email protected] "4"; break; Case 6: [email protected] "5"; break; Case 7: [email protected] "6"; break; default: break;} string = [nsstring stringwithformat: @ "week % @ % d: % 02d", daystr, hour, minute];} else if (year = t_year) string = [nsstring stringwithformat: @ "% d month % d", month, day]; else string = [nsstring stringwithformat: @ "% d Year % d month % d", ye Ar, month, day]; return string;} + (INT) daycountformonth :( INT) month andyear :( INT) year {If (month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10 | month = 12) {return 31;} else if (month = 4 | month = 6 | month = 9 | month = 11) {return 30 ;} else if ([self isleapyear: year]) {return 29;} else {return 28 ;}+ (bool) isleapyear :( INT) year {If (Year % 400 = 0) {return yes;} else {If (Year % 4 = 0 & year % 100! = 0) {return yes;} else {return no; }}@ end


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.