Conversion of dates and strings in iOS

Source: Internet
Author: User

Example 1, the string obtained from the server segment converted to time such as: conversion 1416882712000NSString *time =1416882712000;//time string on the server Nsinteger num = [Timing integervalue]/ 1000; (emphasis) NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease]; [Formatter Setdatestyle:nsdateformattermediumstyle]; [Formatter Settimestyle:nsdateformattershortstyle]; [Formatter Setdateformat: @YYYY-MM-DD]; NSDATE*CONFROMTIMESP = [NSDate datewithtimeintervalsince1970:num]; NSSTRING*CONFROMTIMESPSTR = [Formatter Stringfromdate:confromtimesp];cell. Datecontent.text = Confromtimespstr; After the conversion, the result is: 2014-11-25 Example 2, how to convert a string such as "20110826134106" into any date-time format, Two types are listed below: nsstring*string = @20110826134106; NSDateFormatter *inputformatter= [[[[[NSDateFormatter alloc] init] autorelease]; [Inputformatter Setlocale:[[[nslocale alloc] initwithlocaleidentifier: @en_US] autorelease]; [Inputformatter Setdateformat: @yyyyMMddHHmmss]; Nsdate*inputdate = [Inputformatter datefromstring:string]; NSLog (@date =%@, inputdate); NSDateFormatter *outputformatter= [[[[NSDateFormatter alloc] init] autorelease]; [Outputformatter Setlocale:[nslocale Currentlocale]; [Outputformatter Setdateformat: @yyyy years mm month DD day hh mm minutes ss seconds]; NSString *str= [Outputformatter stringfromdate:inputdate]; NSLog (@testDate:%@,STR); two times printed results: date= 2011-08-26 05:41:06 +0000testdate:2011 August 26 13:41 06 seconds

Conversion of dates and strings in iOS

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.