Encapsulate convenient classes for parsing nsdate

Source: Internet
Author: User

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

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.