Object-c Date summary, object-c Summary

Source: Internet
Author: User

Object-c Date summary, object-c Summary
/**
* NSDate and NSDateComponents
*/


/**
* Create a date that identifies today
* Method: Create an object that represents the current date using the date method in NSDate
*/


NSDate * todaysDate = [NSDate date];


/**
* Obtain the date column of today
*/
# Import <Foundation/Foundation. h>
Int main (int argc, const char * argv []) {
@ Autoreleasepool {
NSDate * todaysDate = [NSDate date];
NSLog (@ "Today's date is % @", todaysDate );
}
Return 0;
}


// Print the following message:
Today's date is 13:14:30 + 0000


/**
* Create a custom date
*/
NSDateComponents: Specifies the detailed information about the date and time: day, month, year, and hour.
NSCalendar: a calendar that identifies the real world and is used for complex calendar-related operations.
NSDate: base class
NSDateComponents * dateComponents = [[NSDateComponents alloc] init];
DateComponents. year = 2007;
DateComponents. month = 6;
DateComponents. day = 29;
DateComponents. hour = 12;
DateComponents. minute = 01;
DateComponents. second = 31;
DateComponents. timeZone = [NSTimeZone timeZoneWithAbbreviation: @ "PDT"];


/**
* DEMO
*
/
# Import <Foundation/Foudation. h>
Int main (int argc, const char * argv []) {
@ Autoreleasepool {
NSDateComponents * dateComponents = [[NSDateComponents alloc] init];
DateComponents. year = 2007;
DateComponents. month = 6;
DateComponents. day = 29;
DateComponents. hour = 12;
DateComponents. minute = 01;
DateComponents. second = 31;
DateComponents. timeZone = [NSTimeZone tmeZoneWithAbbreviation: @ "PDT"];
NSDate * iPhoneRelaeaseDate = [[NSCalendar currentCalendar] dateFromComponents: dateComponents];
NSLog (@ "The original iPone went on sale: % @", iPoneReleaseDate );
}
Return 0;
}
// Output information
The original iPone went on sale: 19:01:31 + 0000




/**
* Comparison; two dates
*/
// Equal comparison
NSDate * todaysDate = [NSDate date];
If ([todaysDate is1_todate: iPhoneReleaseDate]) {
NSLog (@ "The iPone was released todya! ");
}
Else {
NSLog (@ "The iPone was released on some other date ");
}
// Determine the date Morning and evening earlierDate
NSDate * earlierDateIs = [todayDate earlierDate: iPoneReleaseDate];
// LaterDate
NSDate * laterDateIs = [todaysDate laterDate: iPoneReleaseDate];
// TimeIntervalSinceDate
NSCalendar * systemCalendar = [NSCalendar currentCalendar];


Usigined int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateCompnents * dateComparisonComponents = [systemCalendar components: unitFlags fromDate: iPoneReleaseDate
ToDate: todaysDate options: NSWrapCalendarComponents];












# Import <Foundation/Foundation. h>
Int main (int argc, const char * argv []) {
@ Autoreleasepool {
NSDateComponents * dateComponents = [[NSDateComponents alloc] init];
DateComponents. year = 2007;
DateComponents. month = 6;
DateComponents. day = 29;
DateComponents. hour = 12;
DateComponents. minute = 01;
DateComponents. second = 31;
DateComponents. timeZone = [NSTimeZone timeZoneWithAbbreviation: @ "PDT"];
NSDate * iPhoneReleaseDate = [[NSCalendar currentCalendar] dateFromComponents: dateComponents];
NSLog (@ "The original iPone went on sale: % @", iPoneReleaseDate );
NSDate * todayDate = [NSDate date];
NSLog (@ "Today's date is: % @", todaysDate );
If ([todaysDate is1_todate: iPoneReleaseDate]) {
NSLog (@ "The iPone was released today! ");
}
Else {
NSLog (@ "The iPone was released today! ");
}
NSDate * earlierDateIs = [todaysDate earlierDate: iPoneReleaseDate];
NSLog (@ "The earlier date id: % @", earlierDateIs );
NSDate * laterDateIs = [todayDate laterDate: iPoneReleaseDate];
NSLog (@ "The later date is: % @", laterDateIs );
NSTimeInterval timeBetweenDates = [todaysDate timeIntervalSinceDate: iPhoneReleaseDate];
NSLog (@ "The iPone was release was released % f seconds ago", timeBetweenDates );
NSCalendar * systemCalendar = [NSCalendar currentCalendar];
Unsigned int unilags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents * dateComparisonComponents = [systemCalendar components: unitFlags fromDate: iPoneReleaseDate toDate: todaysDate options: NSWrapCalendarComponents];
NSLog (@ "The iPone was released % ld years, % ld months and % ld days ago", dateComparisonComponents. year, dateComparisonComponents. month, dateComparisonComponents. day );
}
Return 0;
}


/**
* Convert a string to a date.
*/
// NSDateFormatter
NSString * dateString = @ "02/14/2012"
NSDateFormatter * df = [[NSDateFormatter alloc] init]; // create a formatting Class Object


Df. dateFormat = @ "MM/dd/yyyy"; // set the string format
NSDate * valentinesDay = [df dateFromString: dateString]; // execute the conversion string function to convert the string to the date type


/**
* Date conversion example
*/
Import <Foundation/Foundation. h>
Int main (int argc, const char * argv []) {
@ Autoreleasepooll {
NSString * dateString = @ "02/14/2012 ";
NSDateFormatter * df = [[NSDateFormatter alloc] init];
Df. dateFormat = @ "MM/dd/yyyy ";
NSDate * valentinesDay = [df dateFromString: dateString];
NSLog (@ "Valentine's Day =%@", valentinesDay );
}
Return 0;
}


// Print the information:
Valentine's Day = 05:02:00 + 0000


// Format the date for display:
Df. dateFormat = @ "EEEE, MMMM d ";


/**
* Format a date example
*/
# Import <Foundation/Foundation. h>
Int main (int argc, const char * argv []) {
@ Autoreleasepool {
NSString * dateString = @ "02/14/2012 ";
NSDateFormatter * df = [[NSDateFormatter alloc] init];
Df. dateFormat = @ "MM/dd/yyyy ";
NSDate * valentinesDay = [df dateFromStrign: dateString];
NSLog (@ "Unformatted Valentine's Day = % @", valentinesDay );
NSLog (@ "Formatted Valendtine's Day = % @", [df stringFormDate: valentinesDay]);
}
Return 0;
}


/**
* Addition or subtraction date
*/
// NSDateComponents and NSCalendar NSDateComponents specify the time length
NSDateComponents * weekBeforeDateComponents = [[NSDateComponents alloc] init];
// If you lose one week, set the week attribute in NSDateComponents to-1.
WeekBeforeDateComponents. week =-1;


NSDate * vDayShoppingDay = [[NSCalendar currentCalendar]
DateByAddingComponents: weekBeforeDateComponents toDate: valentinesDay options: 0];


/**
* Date addition and subtraction example
*/
# Import <Foundation/Foundation. h>
Int main (int argc, const char * arv []) {
@ Autoreleasepool {
NSString * dateString = @ "02/14/2012 ";
NSDateFormat * df = [[NSDateFormatter alloc [init];
Df. dateFormat = @ "MM/dd/yyyy ";
NSDate * valentineDay = [df dateFromString: dateString];
NSLog (@ "valentines's Day =%@", valentinesDay );
NSDateComponents * weekBeforeDateComponents = [[NSDateComponents alloc] init];
WeekBeforeDateComponents. week =-1;
NSDate * vDayShopingDay = [[NSCalendar currentCalendar] dateByAddingComponetns: weekBeforeDateComponents toDate: valentinesDay optioins: 0];
NSLog (@ "Shop for Valentine's Day by % @", vDayShoppingDay );
}
Return 0;
}


/**
* Use a timer to schedule and execute tasks repeatedly
*/
// Method for repeated tasks at specific time: NSTimer
// Set the time interval: dateWithTimeIntervalSinceNow: 10.0
NSDate * scheduledTime = [NSDate dateWithTimeINtervalSinceNow: 10.0];


NSString * customUserObject = @ "To demo userInfo ";
NSTimer * timer = [[NSTimer alloc] initWithFireDate: scheduledTime
Interval: 2
Target: self
Selector: @ selector (task)
UserInfo: customUserObject
Repeats: YES];


// Parameter description:
Parameter 1: The date object specifies when the timer changes to the active state.
Parameter 2: Next is the interval, which is the number of seconds that the timer needs to wait before sending the message again.
Parameter 3: target parameter descriptor the object of the method. Because the method and the timer are both in the monthly application delegate, the self keyword can be used.
Parameter 4: The selector parameter is located before the method name in parentheses and is the @ selector keyword.
Parameter 5: userInfo is the custom content used by the timer. You can use any object and get the object reference in the message being executed (the selector parameter above ). Strings are used here, But dictionaries or other sets are usually used to support more complex activities.
Parameter 6: The repeats parameter indicates whether the timer sends a message once, or whether the message is repeatedly sent according to the time interval specified by the 2nd parameters.
Nsunloop * runLoop = [nsunloop currentRunLoop [;
[RunLoop addTimer: timer forMode: NSDefaultRunLoopMode];
After 10 seconds, the timer starts to send task messages to the application every two seconds.
[Timer invalidate];
/**
* Example of a timer
*/
# Import "AppDelegate. h"
@ Implementation AppDelegate
@ Synthesize window = _ window;
-(Void) applicationDidFinishLaunching :( NSNotification *) aNotification {
NSDate * scheduledTime = [NSDate dateWithTimeIntervalSinceNow: 10.0];
NSString * customUserObject = @ "To demo userInfo ";
NSTimer * timer = [[NSTimer alloc] initWithFireDate: scheduledTime
Interval: 2
Target: self
Selector: @ selector (task)
UserInfo: customUserObject
Repeats: YES];
Nsunloop * runLoop = [nsunloop currentRunLoop [;
[RunLoop addTimer: timer forMode: NSDefaultRunLoopMode];
}
-(Void) task :( id) sender {
NSTimer * localTimer = (nstmer *) sender;
NSLog (@ "Schedule task has executed with this user info: % @", [localTimer userInfo]);
}
@ End

Related Article

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.