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