ios-time and string to convert each other

Source: Internet
Author: User

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.


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.