<span id="Label3"></p><p><p>When developing iOS programs, it is sometimes necessary to adjust the time format to the format you want, which we can do with the NSDateFormatter class.</p></p><p><p>For example: How to convert the time format "12-may-14 05.08.02.000000 PM" to "2014/05/12 5:08:02", The following is the specific implementation code:</p></p><p><p>nsstring* timestr = @ "12-may-14 05.08.02.000000 PM";</p></p><p><p>NSDateFormatter *formatter = [[nsdateformatter alloc] init];</p></p><p><p>[formatter setdatestyle:nsdateformattermediumstyle];</p></p><p><p>[formatter settimestyle:nsdateformattershortstyle];</p></p><p><p>[formatter setdateformat:@ "dd-mmmm-yy hh.mm.ss.ssssss aa"];//set The format of the source time string</p></p><p><p>nstimezone* TimeZone = [nstimezone timezonewithname:@ "asia/shanghai"];//set time zone</p></p><p><p>[formatter settimezone:timezone];</p></p><p><p>nsdate* date = [formatter datefromstring:timestr];//converts The source time string to NSDate</p></p><p><p>nsdateformatter* toformatter = [[nsdateformatter alloc] init];</p></p><p><p>[toformatter setdatestyle:nsdateformattermediumstyle];</p></p><p><p>[toformatter settimestyle:nsdateformattershortstyle];</p></p><p><p>[toformatter setdateformat:@ "YYYY/MM/DD h:mm:ss"];//set The format of the target time string</p></p><p><p>NSString *targettime = [toformatter stringfromdate:date];//convert time to target time string</p></p><p><p>NSLog (@ "targettime = = =%@", targettime);</p></p><p><p></p></p><p><p>In fact, the conversion between time formats is entirely dependent on nsdateformatter.</p></p><p><p>NSDateFormatter has the following 2 methods:<br>-(nsstring *) stringfromdate: (nsdate *) date;//nsdate turn NSString<br>-(nsdate *) datefromstring: (nsstring *) string;//nsstring turn NSDate<br></p></p><p><p></p></p>
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.