object-c 日期總結
/**
*NSDate和NSDateComponents
*/
/**
*創建標識今天的日期
*方法:NSDate中的date方法創建表示當前日期的對象
*/
NSDate *todaysDate=[NSDate date];
/**
*獲取今天的日期列子
*/
#import
int main(int argc,const char *argv[]){
@autoreleasepool{
NSDate *todaysDate=[NSDate date];
NSLog(@"Today's date is %@",todaysDate);
}
return 0;
}
//打印消息如下:
Today's date is 2012-6-27 13:14:30 +0000
/**
*自定義日期的創建
*/
NSDateComponents:代表構成日期與時間的細節信息:天、月、年與小時。
NSCalendar:標識真實世界的日歷,用於刮泥與日歷相關的復雜操作
NSDate:基類
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
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;
}
//輸出信息
The original iPone went on sale:2007-06-29 19:01:31 +0000
/**
*比較;兩個日期
*/
//比較相等
NSDate *todaysDate=[NSDate date];
if([todaysDate isEqualToDate:iPhoneReleaseDate]){
NSLog(@"The iPone was released todya!");
}
else {
NSLog(@"The iPone was released on some other date");
}
//判斷日期早晚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
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 isEqualToDate: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 uniFlags=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|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;
}
/**
*將字符串轉換為日期
*/
//NSDateFormatter
NSString *dateString=@"02/14/2012"
NSDateFormatter *df=[[NSDateFormatter alloc]init];//創建格式化類對象
df.dateFormat=@"MM/dd/yyyy";//設置字符串格式
NSDate *valentinesDay=[df dateFromString:dateString];//執行轉換字符串函數將字符串轉換為date類型
/**
*日期轉換例子
*/
import
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;
}
//打印信息:
Valentine's Day =2012-02-14 05:02:00 +0000
//格式化日期以便顯示:
df.dateFormat=@"EEEE,MMMM d";
/**
*格式化日期例子
*/
#import
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;
}
/**
*加減日期
*/
//NSDateComponents、NSCalendar NSDateComponents指定了時間長度
NSDateComponents *weekBeforeDateComponents=[[NSDateComponents alloc]init];
//減掉一周就將NSDateComponents中的week屬性設為-1
weekBeforeDateComponents.week=-1;
NSDate *vDayShoppingDay=[[NSCalendar currentCalendar]
dateByAddingComponents:weekBeforeDateComponents toDate:valentinesDay options:0];
/**
*加減日期例子
*/
#import
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;
}
/**
*使用定時器調度並重復執行任務
*/
//特定時間重復執行任務方式:NSTimer
//設置時間間隔: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];
//參數說明:
參數一:日期對象指定了定時器何時變成活動狀態。
參數二:接下來是間隔時間,是定時器再次發送消息前所需等待的秒數。
參數三:是目標參數描述符目標是方法所處的對象由於方法與定時器一樣都位於月應用委托中,因此可以使用self關鍵字
參數四:selector參數是位於圓括號中的方法名前面是@selector關鍵字。
參數五:userInfo是定時器使用的自定義內容。可以使用任何對象,並且可以獲得正在執行的消息中的對象引用(上面的selector參數)。這裡使用字符串,但通常會使用字典或其他集合以支持更加復雜的活動。
參數六:repeats參數表示定時器是發送一次消息,還是根據第2個參數指定的時間間隔重復發送。
NSRunLoop *runLoop=[NSRunLoop currentRunLoop[;
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
10秒鐘後,定時器將會開始每隔兩秒鐘向應用發送task消息。
[timer invalidate];
/**
*定時器小例子
*/
#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];
NSRunLoop *runLoop=[NSRunLoop currentRunLoop[;
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(void)task:(id)sender{
NSTimer *localTimer=(NSTimer *)sender;
NSLog(@"Schedule task has executed with this user info :%@",[localTimer userInfo]);
}
@end