iOS开发之-Dates and Times

本文深入探讨了Objective-C与Swift两种编程语言在iOS开发领域的使用场景、优势与挑战,提供了开发者选择语言时的重要考量因素,并分享了实际项目中如何高效地运用这两种语言实现高质量的应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Date Fundamentals

1. Cocoa represents dates and times as NSDate objects (Combination of a clock time and a day on a particular calendar).

2. In Cocoa, you use a particular calendar to decompose a date object into its date components such as year, month, day, hour, and minute. Conversely, you can use a calendar to create a date object from date components. 

3. Date objects are immutable. The standard unit of time for date objects is floating point value typed asNSTimeInterval and is expressed in seconds. 

4. NSDate computes time as seconds relative to an absolute reference time: the first instant of January 1, 2001, Greenwich Mean Time (GMT). Dates before then are stored as negative numbers; dates after then are stored as positive numbers. 


Creating Date Objects

1. Get current time

NSDate *now = [[NSDate alloc] init];
NSDate *now = [NSDate date];


2. Get other time

Listing 1  Creating dates with time intervals
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *tomorrow = [[NSDate alloc]
            initWithTimeIntervalSinceNow:secondsPerDay];
NSDate *yesterday = [[NSDate alloc]
            initWithTimeIntervalSinceNow:-secondsPerDay];
[tomorrow release];
[yesterday release];

Listing 2  Creating dates by adding a time interval
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] init];
NSDate *tomorrow, *yesterday;
 
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
[today release];


Basic Date Calculations

if (fabs([date2 timeIntervalSinceDate:date1]) < 60) ...

– timeIntervalSinceDate:
– timeIntervalSinceNow
+ timeIntervalSinceReferenceDate
– timeIntervalSinceReferenceDate


Calendars, Date Components, and Calendar Units

Calendar Basics

Listing 3  Creating calendar objects
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
 
NSCalendar *japaneseCalendar = [[NSCalendar alloc]
                                initWithCalendarIdentifier:NSJapaneseCalendar];
 
NSCalendar *usersCalendar =
                      [[NSLocale currentLocale] objectForKey:NSLocaleCalendar];

Date Components and Calendar Units

1.You represent the component elements of a date—such as the year, day, and hour—using anNSDateComponents object. 


Listing 4  Creating a date components object
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:6];
[components setMonth:5];
[components setYear:2004];
 
NSInteger weekday = [components weekday]; // Undefined (== NSUndefinedDateComponent)

Converting between Dates and Date Components

Listing 5  Getting a date’s components
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                    [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];


Listing 6  Creating a date from components
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:2]; // Monday
[components setWeekdayOrdinal:1]; // The first Monday in the month
[components setMonth:5]; // May
[components setYear:2008];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];


Listing 7  Creating a yearless date
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMonth:11];
[components setDay:7];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *birthday = [gregorian dateFromComponents:components];

Converting from One Calendar to Another


Listing 8  Converting date components from one calendar to another
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:6];
[comps setMonth:5];
[comps setYear:2004];
 
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];
[gregorian release];
 
NSCalendar *hebrew = [[NSCalendar alloc]
                        initWithCalendarIdentifier:NSHebrewCalendar];
NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit |
                              NSYearCalendarUnit;
NSDateComponents *components = [hebrew components:unitFlags fromDate:date];
 
NSInteger day = [components day]; // 15
NSInteger month = [components month]; // 9
NSInteger year = [components year]; // 576





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值