iOS Intermediate-Date Time objects

Source: Internet
Author: User
Tags date1 dateformat

Combine Nscalendar and nsdate to do more date \ Time processing to get Nscalendar objects

Nscalendar *calendar = [Nscalendar Currentcalendar];? Acquired date

-(Nsdatecomponents *) components: (nscalendarunit) unitflags fromdate: (nsdate *) date;

Date Created
Nsdate?*d = [Nsdate?date];

Create Date Object
NSCALENDAR?*CA = [Nscalendar?currentcalendar];

Get Time component

nsdatecomponents?*cms= [ca?components:nscalendarunityear| nscalendarunitmonth| Nscalendarunitday Fromdate:d];

NSLog (@ "%ld-%ld-%ld", cms.year,cms.month,cms.day);

Compare the gap between two dates
-(Nsdatecomponents *) components: (nscalendarunit) unitflags fromdate: (nsdate *) startingdate toDate: (NSDate *) Resultdate options: (nscalendaroptions) opts;

NSString *time1 [email protected] "2014-04-08 20:50:40"; NSString *time2 [email protected] "2014-04-04 18:45:30";

NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; Fmt.dateformat [email protected] "YYYY-MM-DD HH:mm:ss";


nsdate *date1 = [FMT datefromstring:time1]; NSDate *date2 = [FMT datefromstring:time2];
?//Take the time earlier??? [Date2?earlierdate:date1];? ? ? ?? Take the later one??? [Date2?laterdate:date1];
Convert a string to a date
? ? ? ? Similar to the top, the date format object is still required, but the method of invocation changes from Stringfromdate to
? ? ? ? This date was subtracted from the day because of the time zone setting problem
? ? ? ? Nsdateformatter?*format2=[[nsdateformatter?alloc]init];
? ? ? ? [format2?setdateformat:@ "Yyyy/mm/dd"];
? ? ? ? Nsstring?*[email protected] "2014/10/11";
? ? ? ? NSLog (@ "%@", [FORMAT2?DATEFROMSTRING:STR2]);

1. Create a Calendar object
Nscalendar?*calendar = [Nscalendar?currentcalendar];

2. Compare the time gap
Int?unit =? Nscalendarunityear?|? Nscalendarunitmonth?|? Nscalendarunitday
|? Nscalendarunithour?|? Nscalendarunitminute?|? Nscalendarunitsecond;?

Nsdatecomponents?*cmps = [calendar?components:unit?fromdate:date2?todate:date1?options:0];


NSLog (@ "%ld years%ld"%ld days%ld hours%ld minutes%ld seconds ", Cmps.year, Cmps.month, Cmps.day, Cmps.hour, Cmps.minute, cmps.second);
Calculate Date//1) The moment of Tomorrownstimeinterval secondsperday = 24*60*60;
NSDate *tomorrow = [NSDate datewithtimeintervalsincenow:secondsperday]; NSLog (@ "mydate =%@", tomorrow);

2) yesterday's present
Nstimeinterval secondsPerDay1 = 24*60*60;

nsdate *now = [NSDate date];nsdate *yesterday = [now addtimeinterval:-secondsperday1]; NSLog (@ "Yesterday =%@", yesterday);

Formatted date

Define NSDate
NSDATE?*D1 = [Nsdate?date];

Define a date time formatted class
Nsdateformatter?*formatter = [[Nsdateformatter?alloc]?init]; [Email protected] "YYYY-MM-DD HH:mm:ss";

Convert Date to Datastr
NSSTRING?*DATESTR = [FORMATTER?STRINGFROMDATE:D1];

  • //?? Set format

  • //?? Fmt.dateformat = @ "Today?" Hh:mm ";

  • //?? Hh:24 Hour System

  • //?? Hh:12 Hour System

  • //?? YYYY:? year

  • //?? MM:? month

  • //?? DD: No.

  • //?? MM:? minutes

  • //?? SS:? seconds

  • //?? Z:? Time Zone

iOS Intermediate-Date Time objects

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.