In development, you often encounter the conversion of time format when interacting with the background.
First, several common nsdate formats are listed:
1. 20150605234106 format
2.2015-06-05-15:41:06 +0000 format (normal output format)
3.1433498400 format (number of seconds from 1970 to present)
the first format is converted to the second format
First format of time
nsstring* string = @ "20150605234106";
NSDateFormatter *inputformatter = [[NSDateFormatter alloc] init];
[Inputformatter Setlocale:[[nslocale alloc] initwithlocaleidentifier:@ "en_US"];
(Set the format you want, the difference between HH and HH: 12-hour system, 24-hour system respectively)
[Inputformatter setdateformat:@ "YYYYMMDDHHMMSS"];
nsdate* inputdate = [Inputformatter datefromstring:string];
The output is the date of the second format
NSLog (@ "date =%@", inputdate);
The first format is converted into a third format
Get to the current time
NSDate * nowdate=[nsdate Date];
NSLog (@ "nowdate =%@", nowdate);
The date of the third format is output
NSString * datestring=[nsstring stringwithformat:@ "%.0f", [Nowdate timeIntervalSince1970]];
NSLog (@ "datestring =%@", datestring);
The third format is converted to a string that can be exported
Date of the third format
NSString * datestring= @ "1433487206";
NSDate *updateddate=[[nsdate alloc] initwithtimeintervalsince1970:[datestring Doublevalue]];
The output is a string that can be displayed (a nsdate+category is required here to implement)
NSString * Result=[updateddate timeintervaldescription];
NSLog (@ "result =%@", result); Timeintervaldescription is an optional method that can be exported to date display formats in different formats
-(NSString *) timeintervaldescription;//distance from the current time interval description
-(NSString *) minutedescription;/* accurate to minute date description * *
-(NSString *) formattedtime;
-(NSString *) formatteddatedescription;//Format Date Description
No need for nsdate+category to achieve simple transformations
Long long int date=1433488265;
NSDate * Nowdate=[nsdate Datewithtimeintervalsince1970:date];(not a few minutes before the hint)
Hint; the time taken is calculated in 0 time zones. The time zone needs to be converted when exporting.