码迷,mamicode.com
首页 > 其他好文 > 详细

时间与日期。

时间:2014-08-15 14:32:18      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:使用   os   io   for   ar   代码   时间   log   

1,创建时间。

        NSDate *todaysDate = [NSDate date];

        NSLog(@"Today‘s date is %@", todaysDate);

后面的+0000很讨厌。

2,由组建创建时间段,比如年月日,十分秒。

timeZoneWithAbbreviation设置时区。

具体代码

        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:@"GMT"];

        

        NSDate *iPhoneReleaseDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];

        

 

        NSLog(@"The original iPhone went on sale: %@", iPhoneReleaseDate);

 

3,比较两个时区。

比如看看是不是今天。

        NSDate *todaysDate = [NSDate date];

        if([todaysDate isEqualToDate:iPhoneReleaseDate])

            NSLog(@"The iPhone was released today!");

        else

 

            NSLog(@"The iPhone was released on some other date”);

早一天或晚一天。

NSDate *earlierDateIs = [todaysDate earlierDate:iPhoneReleaseDate];

 

NSDate *laterDateIs = [todaysDate laterDate:iPhoneReleaseDate];

日历

NSCalendar *systemCalendar = [NSCalendar currentCalendar];

 

unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |

        NSDayCalendarUnit;

具体代码

        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 iPhone went on sale: %@", iPhoneReleaseDate);

        NSDate *todaysDate = [NSDate date];

        NSLog(@"Today‘s date is: %@", todaysDate);

        if([todaysDate isEqualToDate:iPhoneReleaseDate])

            NSLog(@"The iPhone was released today!");

        else

            NSLog(@"The iPhone was released on some other date");

        NSDate *earlierDateIs = [todaysDate earlierDate:iPhoneReleaseDate];

        NSLog(@"The earlier date is: %@", earlierDateIs);

        NSDate *laterDateIs = [todaysDate laterDate:iPhoneReleaseDate];

        NSLog(@"The later date is: %@", laterDateIs);

        NSTimeInterval timeBetweenDates = [todaysDate timeIntervalSinceDate:iPhoneReleaseDate];

        NSLog(@"The iPhone was released %f seconds ago", timeBetweenDates);

        NSCalendar *systemCalendar = [NSCalendar currentCalendar];

        unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit| NSDayCalendarUnit;

        NSDateComponents *dateComparisonComponents = [systemCalendar components:unitFlags

                                                                        fromDate:iPhoneReleaseDate toDate:todaysDate

                                                                         options:NSWrapCalendarComponents];

        NSLog(@"The iPhone was released %ld years, %ld months and %ld days ago",

              dateComparisonComponents.year,

              dateComparisonComponents.month,

              dateComparisonComponents.day

              );

你可以比较出差多少十分秒,年月日等。

4,字符串转换成日期。

       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);

dateFromString:dateString就行了。

5,

按一定格式格式化日期。

        NSString *dateString = @"02/14/2012";

 

        NSDateFormatter *df = [[NSDateFormatter alloc] init];

 

        df.dateFormat = @"MM/dd/yyyy";

 

        NSDate *valentinesDay = [df dateFromString:dateString];

 

        NSLog(@"Unformatted Valentine‘s Day = %@", valentinesDay);

 

        NSLog(@"Formatted Valentine‘s Day = %@", [df stringFromDate:valentinesDay]);

 

        df.dateFormat = @"EEEE, MMMM d";

 

        NSLog(@"Another Formatted Valentine‘s Day = %@", [df stringFromDate:valentinesDay]);

6,加减日期。

代码,

        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);

 

        NSDateComponents *weekBeforeDateComponents = [[NSDateComponents alloc] init];

 

        weekBeforeDateComponents.week = -1;

 

        NSDate *vDayShoppingDay = [[NSCalendar currentCalendar] dateByAddingComponents:weekBeforeDateComponents

 

                                                                                 toDate:valentinesDay

 

                                                                                options:0];

 

        NSLog(@"Shop for Valentine‘s Day by %@", vDayShoppingDay);

由于日期是对象,操作日期的相关属性即可,

7,使用定时器安排或重复任务。就是你想要一段代码在特定时期执行。

先弄一个日期对象

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];

具体代码

 

#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

 

自己建立,是mac应用,不是IOS应用。

时间与日期。,布布扣,bubuko.com

时间与日期。

标签:使用   os   io   for   ar   代码   时间   log   

原文地址:http://www.cnblogs.com/guanliyang/p/3914708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!