IOS-string converted to nsdate type calculation is different from the current time by the number of days in a month

Source: Internet
Author: User

Nsstring * datestr = [DIC
Objectforkey: @ "date"]; // 2012-05-17 11:23:23


Nscalendar * Gregorian = [[nscalendar
Alloc]
Initwithcalendaridentifier: nsgregoriancalendar];


Nsuinteger unitflags =
Nsmonthcalendarunit | nsdaycalendarunit;


Nsdateformatter * format = [[nsdateformatter
Alloc]
Init];

[Format
Setdateformat: @ "yyyy-mm-dd hh: mm: SS"];


Nsdate * fromdate = [format datefromstring: datestr];


Nstimezone * fromzone = [nstimezone
Systemtimezone];


Nsinteger frominterval = [fromzone secondsfromgmtfordate: fromdate];


Nsdate * fromdate = [fromdate datebyaddingtimeinterval: frominterval];


Nslog (@ "fromdate = % @", fromdate );

[Format
Release];


Nsdate * Date = [nsdate
DATE];


Nstimezone * zone = [nstimezone
Systemtimezone];


Nsinteger interval = [zone secondsfromgmtfordate: date];


Nsdate * localedate = [date datebyaddingtimeinterval: interval];


Nslog (@ "enddate = % @", localedate );


Nsdatecomponents * components = [Gregorian components: unitflags
Fromdate: fromdate
Todate: localedate options: 0];


Nsinteger months = [components month];


Nsinteger days = [components Day]; // year [components year]


Nslog (@ "month = % d", months );


Nslog (@ "days = % d", days );

[Gregorian
Release];


If (months = 0 & days = 0 ){

Datestr = [[datestr
Substringfromindex: 11] substringtoindex: 5];

Cell. textlabel. Text = [nsstring
Stringwithformat: @ "today % @", datestr]; // Today

} Else
If (months = 0 & days = 1 ){

Datestr = [[datestr
Substringfromindex: 11] substringtoindex: 5];


Cell. textlabel. Text = [nsstring
Stringwithformat: @ "Yesterday % @", datestr]; // Yesterday

} Else {

Datestr = [datestr
Substringtoindex: 10];


Cell. textlabel. Text = datestr;

}

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.