iOS development nsdateformatter,nsdate and NSString date conversion, nsstring any format

Source: Internet
Author: User

Generally written in java+oracle background, may give you back the time style is this:

2014-01-20 13:24:33

Look at this time string is more complex, but the iOS SDK has been packaged for us to convert NSString to any nsdate or nsstring

Using the NSDateFormatter implementation

+ (NSString *) timefromstring: (NSString *) timestring{    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];< C1/>[formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];//Enter date format    nsdate *date = [Formatter datefromstring: timestring];//from NSString get a nsdate    nsdateformatter *formatter2 = [[NSDateFormatter alloc] init];    [Formatter2 setdateformat:@ "Yyyy/mm/dd"];           Output Date format    nsstring *str = [NSString stringwithformat:@ "%@",                     [Formatter2 stringfromdate:date]];// Convert from NSDate to nsstring        return str;}

in this way, you can convert any received background time string to the type of string you want!

Special attention:

HH rep, 24-hour system

HH stands for 12-hour (if 24-hour time, when converted to HH, a null value is generated)

Original address:

Http://blog.csdn.net/yangbingbinga

iOS development nsdateformatter,nsdate and NSString date conversion, nsstring any format

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.