IOS---Date-time format conversions

Source: Internet
Author: User

1, how to put a string such as "20110826134106" into any date-time format, the following list of two types:nsstring* string [email protected] "20110826134106";
Nsdateformatter*inputformatter = [[[NSDateFormatter alloc] init]autorelease];

[Inputformattersetlocale:[[[nslocale Alloc] Initwithlocaleidentifier: @ "en_US"]autorelease]];
[inputformattersetdateformat:@ "Yyyymmddhhmmss"];
Nsdate*inputdate = [Inputformatter datefromstring:string];
NSLog (@ "date=%@", inputdate);

Nsdateformatter*outputformatter = [[[NSDateFormatter alloc] init]autorelease];
[Outputformattersetlocale:[nslocale Currentlocale]];
[outputformattersetdateformat:@ "yyyy mm month DD day hh" mm min ss seconds "];
NSSTRING*STR = [Outputformatter stringfromdate:inputdate];
NSLog (@ "testdate:%@", str);
two times the result of this printing is:
   date= 2011-08-26 05:41:06 +0000
   testdate:2011 August 26 13:41 06 seconds

2. Ios-nsdateformatter Format Description:
G: A.D., e.g. AD A.D.
YY: The latter 2 digits of the year
YYYY: Full year
MM: Month, display as 1-12
MMM: Month, abbreviated for the English month, such as Jan
MMMM: Month, display full name of the English month, such as Janualy
 DD: Day, 2 digits, such as
D: Day, 1-2-bit display, such as 2
 EEE: Abbreviated day of the week, such as Sun
EEEE: Full written day of the week, such as Sunday
AA: Last afternoon, AM/PM
H: When, 24-hour system, 0-23
K: When, 12-hour, 0-11
M: minutes, 1-2-bit
MM: Min., 2-bit
S: seconds, 1-2 bits
SS: Seconds, 2 bits
S: MS

Common Date Structure:
Yyyy-mm-dd HH:mm:ss. Sss
YYYY-MM-DD HH:mm:ss
Yyyy-mm-dd
MM DD yyyy

3: Sina Weibo interface, get date string conversion

nsstring*datestr = @ "Wed May 2 22:27:08+0800 ";

nsdateformatter* formater =[[nsdateformatter alloc] init];

[Formater setdateformat:@ "EEE MMM D HH:mm:sszzzz yyyy"];
nsdate* date = [Formaterdatefromstring:datestr];
NSLog (@ "%@", date);
   
//nsdate* now = [Nsdatenow];
Double inter = Fabs ([Datetimeintervalsincenow]);
if (Inter <60)
NSLog ( @ "1 mins ago!");
else if (inter< 60*60)
NSLog ( @ "1 hours ago!");
else if (inter< 60*60*24)
NSLog ( @ "1 days ago!");
    NSLog (@ "interval is%f min", inter/60);

The above code in the real machine run, found that the date is null, the simulator normal display, Internet search found that need to set local, sure enough, the real machine normal

nslocale* local =[[[nslocale alloc]initwithlocaleidentifier: @ "en_US"] Autorelease];

[Formatter setlocale:local];

4. Custom Displayed week format

when using the NSDateFormatter conversion date, the day of the week that gets the English alphabet can only be like this, such as sun, Mon, etc.

If you want the day of the week for capital letters, you can:


Nsarray*weekdayary = [Nsarray arraywithobjects:@ "SUN", @ " MON ", @" TUE ", @" WED ", @" THU ", @" FRI ", @" SAT ", nil];


Dateformatter = [[NSDateFormatter alloc] init];


[Dateformatter setdateformat:nslocalizedstring (@ "YYYY. MM.dd.eee ", nil)];


// Change the day of the week for the displayed uppercase letters here
[Dateformattersetshortweekdaysymbols:weekdayary ] ;


[Dateformatter Setlocale:[[nslocale alloc] Initwithlocaleidentifier: @ "en_US"]];


Nstring *str= [dateformatter stringfromdate:[ NSDate Date]];

5. calculate distance How much time is there in a day

nsdate* toDate = [[NSDate alloc]initwithstring:@ "2012-9-29 0:0:00 +0600"];

nsdate* startdate = [[Nsdatealloc] init];

nscalendar* Chineseclendar = [[Nscalendar alloc]initwithcalendaridentifiEr:nsgregoriancalendar];

Nsuinteger unitflags =

Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit | Nsdaycalendarunit | Nsmonthcalendarunit | Nsyearcalendarunit;

Nsdatecomponents *cps = [Chineseclendar components:unitFlagsfromDate:startDate todate:todate   options:0];

Nsinteger diffhour = [cps hour];

Nsinteger diffmin = [Cpsminute];

Nsinteger diffsec = [cps second];

Nsinteger diffday = [cps day];

Nsinteger Diffmon = [cps month];

Nsinteger diffyear = [cps year];

NSLog ( @ "from-now-%@, diff:years:%d Months:%d, days;%d, Hours:%d, mins:%d,se c:%d ",

[toDate description], diffyear, Diffmon, Diffday, diffhour,diffmin,diffsec);

   

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.