Conversion between IOS NSDate and NSString

Source: Internet
Author: User

If we need to use the current time as a string as the name of a picture, we need to convert the current time NSDate type data into the NSString type.

or in the network request, our time on the network is a string but locally we need to output the nsdate type of data.

So providing two methods the first is to convert data of type nsdate to nsstring type data, the second is to convert nsstring type data into NSDate type data

1.NSDate-NSString

// Get current Time NSDate *date = [NSDate date]; // Set Date format nsdateformatter* formatter1 = [[NSDateFormatter alloc] init];[ Formatter1 Setdateformat:@ "yyyymmddhhmmss"]; // becomes a number nsstring* str = [Formatter1 stringfromdate:date]; NSLog (@ "datestring =%@", str);

2.NSString-NSDate

//Time StringNSString *str =@"20150806070733";//Specified time formatnsdateformatter* formatter =[[NSDateFormatter alloc] init]; [Formatter Setdateformat:@"YYYYMMDDHHMMSS"];//Set time zone global standard Time cut must set we want to set the time zone of ChinaNstimezone *zone = [[Nstimezone alloc] initwithname:@ "CUT"];[Formatter settimezone:zone];//change back to date formatNSDate *stringdate =[Formatter datefromstring:str]; NSLog (@"stringdate =%@", stringdate);

Conversion between IOS NSDate and NSString

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.