Sometimes there are problems that need to convert time and time stamps to each other.
such as converting the current time to a format like "20140716155436"
or convert "20140716155436" to "2014-07-16 15:54:36".
First comes the first one:
The current time is converted to a format such as "20140716155436"
///////////////////////////////
// get current time
nsdate * today = [nsdatedate];
NSLog (@ "%@", today);
Print out is "2014-07-16 07:54:36 +0000"
That's weird? It's 2014-07-16 15:54:36.
Due to the existence of different time zones, the system default GMT
So we're going to convert the time format.
// convert time format
nsdateformatter *df = [[nsdateformatteralloc] init]; // Formatting
[DF Setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];
nsstring * S1 = [df stringfromdate: Today];
NSLog(@ "s1s1s1s%@", S1);
print Out is " 2014-07-16 15:54:36 "
Now gets to the current time
It's not over yet, it needs to be converted into a string, like "20140716052021" timestamp
nsdate * date = [DF datefromstring: S1];
// convert time format
nsdateformatter *df2 = [[nsdateformatteralloc] init]; // formatting
[df2 setdateformat:@ "YYYYMMDDHHMMSS"];
[ df2 setLocale: [[nslocalealloc] initwithlocaleidentifier: @ "ZH_CN"];
NSLog (@ "%@", [df2stringfromdate:d ate]);
print Out is "20140716155436"
And Then there's the second one:
Convert "20140716155436" to "2014-07-16 15:54:36"
The first thing to do is convert 20140716155436 into a time format .
// convert time format
nsdateformatter *df = [[nsdateformatteralloc] init]; // Formatting
[DF Setdateformat:@ "YYYYMMDDHHMMSS"];
[DF setLocale: [[nslocalealloc] initwithlocaleidentifier: @ "ZH_CN"];
nsdate *date =[[nsdatealloc]init];
Date =[dfdatefromstring: bill_timestr];
str = [nsstringstringwithformat:@ "%@", Date];
The result of STR is "2014-07-16 07:54:36 +0000"
It's going to take one more turn.
//
nsdateformatter * df2 = [[nsdateformatteralloc] init];
[df2 setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];
nsstring * str1 = [df2 stringfromdate:d ate];
The result of STR1 is "2014-07-16 15:54:36"
OK, you are done.