Create a new NSString (time) classification, creating a conversion of class method implementation times
. h file
#import <Foundation/Foundation.h>@interface nsstring (kjtime)// Timestamp---> Time + (NSString *) Transtotime: (NSString *) Timsp; // Timestamp ---> Date + (NSString *) Transtodate: (NSString *) Timsp; // time----> timestamp + (NSString *) Transtotimesp: (NSString *) time; @end
. m file
#import "nsstring+kjtime.h"@implementationnsstring (kjtime)//time Stamp---> Time+ (NSString *) Transtotime: (NSString *) timsp{Nstimeinterval Time=[timsp Doublevalue];//If you do not use the local time zone, because the jet lag is 8 hours. = = 28800 secNSDate *detaildate=[NSDate Datewithtimeintervalsince1970:time]; //instantiate a NSDateFormatter objectNSDateFormatter *dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Settimezone:[nstimezone Localtimezone];//set local time zone//Set the time format, where you can set the format you want[Dateformatter Setdateformat:@"MM/DD hh:mm"]; NSString*currentdatestr =[Dateformatter stringfromdate:detaildate]; returncurrentdatestr;}//Timestamp ---> Date+ (NSString *) Transtodate: (NSString *) timsp{Nstimeinterval Time=[timsp Doublevalue];//If you do not use the local time zone, because the jet lag is 8 hours. = = 28800 secNSDate *detaildate=[NSDate Datewithtimeintervalsince1970:time]; //instantiate a NSDateFormatter objectNSDateFormatter *dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Settimezone:[nstimezone Localtimezone];//set local time zone//Set the time format, where you can set the format you want[Dateformatter Setdateformat:@"mm month DD Day"]; NSString*currentdatestr =[Dateformatter stringfromdate:detaildate]; returncurrentdatestr;}//time----> Time stamp+ (NSString *) Transtotimesp: (NSString *) time{NSDateFormatter*dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Settimezone:[nstimezone Localtimezone]; //set local time zone[Dateformatter Setdateformat:@"YYYY-MM-DD hh:mm"]; NSDate*date =[Dateformatter Datefromstring:time]; NSString*TIMESP = [NSString stringWithFormat:@"%ld", (Long) [date timeIntervalSince1970]];//time Stamp returnTimesp;}@end
IOS: Time formatting (standard time to timestamp, timestamp to standard time, timestamp to date)