iOS time format conversion

Source: Internet
Author: User
Tags month name
<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 PM" to "2014/05/12 5:08:02", The following is the specific implementation code:</p></p><p><p>nsstring* timestr = @ "12-may-14 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 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>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.