Encapsulate convenient classes for parsing nsdate
This class can be parsed from nsdate year, month, date, hour, minute, second, millisecond, enough to do many things, the source code is provided as follows:
The following are core classes:
Timeinfo. h and timeinfo. m
//// Timeinfo. h // Showtime /// created by youxianming on 14-10-16. // copyright (c) 2014 youxianming. all rights reserved. // # import <Foundation/Foundation. h> @ Class humantimeinfo; @ interface timeinfo: nsobject + (humantimeinfo *) humancanunderstandfromdate :( nsdate *) date; @ end
//// Timeinfo. M // Showtime /// created by youxianming on 14-10-16. // copyright (c) 2014 youxianming. all rights reserved. // # import "timeinfo. H "# import" humantimeinfo. H "static nsdateformatter * _ dateformatter; @ implementation timeinfo + (void) initialize {If (Self = [timeinfo class]) {_ dateformatter = [[nsdateformatter alloc] init]; _ dateformatter. locale = [[nslocale alloc] initwithlocaleidentifier: @ "En_GB"]; _ dateformatter. timezone = [nstimezone timezonewithname: @ "GMT"]; _ dateformatter. dateformat = @ "yyyy: mm: DD: Mmm: Mmmm: hh: mm: SS: AA: EEE: eeee: SSS"; // ssss }}+ (humantimeinfo *) humancanunderstandfromdate :( nsdate *) Date {If (date! = Nil) {nsarray * timeinfoarray = [[_ dateformatter stringfromdate: DATE] componentsseparatedbystring: @ ":"]; humantimeinfo * info = [humantimeinfo new]; info. year = timeinfoarray [0]; info. mounth = timeinfoarray [1]; info. day = timeinfoarray [2]; info. enmounth = timeinfoarray [3]; info. fullenmounth = timeinfoarray [4]; info. hour = timeinfoarray [5]; info. min = timeinfoarray [6]; info. SEC = timeinfoarray [7]; info. ampm = timeinfoarray [8]; info. enweakday = timeinfoarray [9]; info. fullweakday = timeinfoarray [10]; info. MSEC = timeinfoarray [11]; return Info;} else {return nil ;}}@ end
Information that humans can understand:
Humantimeinfo. h and humantimeinfo. m
//// Humantimeinfo. h // Showtime /// created by youxianming on 14-10-16. // copyright (c) 2014 youxianming. all rights reserved. // # import <Foundation/Foundation. h> @ interface humantimeinfo: nsobject @ property (nonatomic, strong) nsstring * year; // 2014 @ property (nonatomic, strong) nsstring * mounth; // 10 @ property (nonatomic, strong) nsstring * day; // 16 @ property (nonatomic, strong) nsstring * enmounth; // Oct @ property (nonatomic, strong) nsstring * fullenmounth; // October @ property (nonatomic, strong) nsstring * hour; @ property (nonatomic, strong) nsstring * min; @ property (nonatomic, strong) nsstring * sec; @ property (nonatomic, strong) nsstring * ampm; // morning or afternoon @ property (nonatomic, strong) nsstring * enweakday; @ property (nonatomic, strong) nsstring * fullweakday; @ property (nonatomic, strong) nsstring * msec; // millisecond @ end
//// Humantimeinfo. M // Showtime /// created by youxianming on 14-10-16. // copyright (c) 2014 youxianming. all rights reserved. // # import "humantimeinfo. H "@ implementation humantimeinfo @ end
Nsdate + currenttime. h and nsdate + currenttime. m
/// Nsdate + currenttime. h // Showtime /// created by youxianming on 14-10-16. // copyright (c) 2014 youxianming. all rights reserved. // # import <Foundation/Foundation. h> @ Class humantimeinfo; @ interface nsdate (currenttime) + (humantimeinfo *) currenttime; + (humantimeinfo *) datefrom :( nsdate *) date; @ end
/// Nsdate + currenttime. M // Showtime /// created by youxianming on 14-10-16. // copyright (c) 2014 youxianming. all rights reserved. // # import "nsdate + currenttime. H "# import" humantimeinfo. H "# import" timeinfo. H "@ implementation nsdate (currenttime) + (humantimeinfo *) currenttime {return [timeinfo humancanunderstandfromdate: [nsdate date];} + (humantimeinfo *) datefrom :( nsdate *) date {return [timeinfo humancanunderstandfromdate: DATE] ;}@ end
If it is used, give the nsdate and parse it out, which is so simple :)
Encapsulate convenient classes for parsing nsdate