> > 取当前时间的秒数 > NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; > long long int date = (long long int)time; > NSLog(@”date\n%d”, date); //1295322949 > //把秒数转化成yyyy-MM-dd hh:mm:ss格式 > NSDate *dd = [NSDate dateWithTimeIntervalSince1970:date]; > NSLog(@”d:%@”,dd); //2011-01-18 03:55:49 +0000 > //给一个时间秒数,取出对应的时间 > NSString *s = @”1295355600000″; //对应21:00 > NSDate *d = [NSDate dateWithTimeIntervalSince1970:[s doubleValue]/1000]; > NSLog(@”dddd:%@”,d); //2011-01-18 13:00:00 +0000 > NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init]; > [formatter1 setDateFormat:@"HH:mm"]; > NSString *showtimeNew = [formatter1 stringFromDate:d]; > NSLog(@”showtimeNew:%@”,showtimeNew); //21:00 比d的时间 +8小时 > [formatter1 release]; > > 1. 建立或初始化可用如下方法 > 用于建立NSDate实例的类方法有 > + (id)date; > 返回当前时间 > + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; > 返回以当前时间为基准,而后过了secs秒的时间 > + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs; > 返回以2001/01/01 GMT为基准,而后过了secs秒的时间 > + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs; > 返回以1970/01/01 GMT为基准,而后过了secs秒的时间 > + (id)distantFuture; > 返回不少年之后的将来的某一天。(好比你须要一个比如今(Now)晚(大)很长时间的时间值,则能够调用该方法。测试返回了4000/12/31 16:00:00) > + (id)distantPast; > 返回不少年之前的某一天。(好比你须要一个比如今(Now)早(小)大很长时间的时间值,则能够调用该方法。测试返回了公元前0001/12/31 17:00:00) > 用于建立NSDate实例的实例方法有 > - (id)addTimeInterval:(NSTimeInterval)secs; > 返回以目前的实例中保存的时间为基准,而后过了secs秒的时间 > 用于初始化NSDate实例的实例方法有 > - (id)init; > 初始化为当前时间。相似date方法 > 初始化为以2001/01/01 GMT为基准,而后过了secs秒的时间。相似dateWithTimeIntervalSinceReferenceDate:方法 > - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate; > 初始化为以refDate为基准,而后过了secs秒的时间 > - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; > 初始化为以当前时间为基准,而后过了secs秒的时间 > 2. 日期之间比较可用如下方法 > - (BOOL)isEqualToDate:(NSDate *)otherDate; > 与otherDate比较,相同返回YES > - (NSDate *)earlierDate:(NSDate *)anotherDate; > 与anotherDate比较,返回较早的那个日期 > - (NSDate *)laterDate:(NSDate *)anotherDate; > 与anotherDate比较,返回较晚的那个日期 > - (NSComparisonResult)compare:(NSDate *)other; > 该方法用于排序时调用: > . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame > . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending > . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending > 3. 取回时间间隔可用如下方法 > - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate; > 以refDate为基准时间,返回实例保存的时间与refDate的时间间隔 > - (NSTimeInterval)timeIntervalSinceNow; > 以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔 > - (NSTimeInterval)timeIntervalSince1970; > 以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔 > - (NSTimeInterval)timeIntervalSinceReferenceDate; > 以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔 > + (NSTimeInterval)timeIntervalSinceReferenceDate; > 以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔 > 4. 将时间表示成字符串 > - (NSString *)description; > 以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。(其中 “±HHMM” 表示与GMT的存在多少小时多少分钟的时区差别。好比,若时区设置在北京,则 “±HHMM” 显示为 “+0800″) > > 将时区设成本地: > NSTimeZone *zone = [NSTimeZonesystemTimeZone]; > > NSInteger interval = [zone secondsFromGMTForDate:date]; > > NSDate *localeDate = [date addTimeInterval:interval]; > > > NSString和NSDate互转须要用到NSDateFormatter,设置一下timezone和format便可,直接上代码 > > > > > > NSString和NSDate互转须要用到NSDateFormatter,设置一下timezone和format便可,直接上代码 > [cpp] view plaincopy > NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; > NSTimeZone *timeZone = [NSTimeZone localTimeZone]; > > [formatter setTimeZone:timeZone]; > [formatter setDateFormat : @"M/d/yyyy h:m a"]; > > NSString *stringTime = @"12/5/2011 3:4 am"; > > NSDate *dateTime = [formatter dateFromString:stringTime]; > > NSLog(@"%@", dateTime);//打印2011-12-04 19:04:00 +0000,这里+0000表示时区 > > NSDate *dateNow = [NSDate date]; > > NSLog(@"%@", dateNow);//打印2011-08-17 08:26:57 +0000,这里+0000表示时区 > > [formatter setDateFormat : @"yyyy年M月d日 H点m分"]; > > NSLog(@"%@", [formatter stringFromDate:dateNow]);//打印2011年8月17日 16点26分 > > 关于format中的每一个字母表明的意思,能够到这里去查:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/df100103.html#//apple_ref/doc/uid/TP40007972-SW1,拉到中间的部分便可看到 > > > 今天发现用NSDateFormatter的stringFromDate方法将服务器传过来的背景时间的NSDate转换成NSString会相差8小时,调查发现,多是由于stringFromDate时,将date当作格林威治时间了,返回的String变成本地时区。因此差了8个小时。 > > 我用的方式是: > > 1 > NSDateFormatter* formate=[[NSDateFormatter alloc]init]; > 2 > [formate setDateFormat:DATE_FORMAT_SPLIT]; > 3 > [formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; > 4 > [formate stringFromDate:date]这样再转换时就不会出现时区问题了。 > 固然,好像方法不是惟一的。 > > > > 1.4.2 日期和时间 > > 咱们使用NSDate类比较日期,并计算两个日期之间的日期和时间间隔: > > 能够用当前的日期和时间建立一个NSDate: > > NSDate *myDate = [NSDate date]; > 能够建立一个NSDate,表示从如今开始的24小时: > > NSTimeInterval secondsPerDay = > > 4*60*60; > NSDate *tomorrow = [NSDate > -ateWithTimeIntervalSinceNow: > -econdsPerDay]; > 可使用以下代码,根据一个已有的日期建立一个日期: > > NSTimeInterval secondsPerDay = > > 4*60*60; > NSDate *now = [NSDate date]; > NSDate *yesterday = [now > -ddTimeInterval:-secondsPerDay]; > 能够比较两个日期是否彻底相等: > > BOOL sameDate = > -date1 isEqualToDate:date2]; > 或者,判断一个日期是在另外一个日期以前仍是以后,使用以下代码: > > NSDate *earlierDate = > -date1 earlierDate:date2]; > NSDate *laterDate = > -date1 laterDate:date2]; > 能够计算两个日期之间相隔多少秒: > > NSTimeInterval secondsBetweenDates = > > > -date2 timeIntervalSinceDate: > -ate1]; > 或者能够计算如今和未来的一个日期之间相隔多少秒: > > NSTimeInterval secondsUntilTomorrow > [tomorrow timeIntervalSinceNow]; > 经过使用NSCalendar类,咱们能够更加容易地建立NSDate对象。 > > 例如,建立一个表示2010年6月1日的日期,使用以下代码: > > NSDateComponents *comp = > -[NSDateComponents alloc] init]; > [comp setMonth:06]; > [comp setDay:01]; > [comp setYear:2010]; > NSCalendar *myCal = [[NSCalendar > -lloc] initWithCalendarIdentifier: > -SGregorianCalendar]; > NSDate *myDate = > -myCal dateFromComponents:comp]; > 相似地,从一个已有的日期中获取日期、月份和年份等组成部分,使用以下代码: > > unsigned units = NSMonthCalendarUnit > - NSDayCalendarUnit | > NSYearCalendarUnit; > NSDate *now =[NSDate date]; > NSCalendar *myCal = [[NSCalendar > -lloc] initWithCalendarIdentifier: > -SGregorianCalendar]; > NSDateComponents *comp = [myCal > -omponents:units fromDate:now]; > NSInteger month = [comp month]; > NSInteger day = [comp day]; > NSInteger year = [comp year]; > 当经过已有的日期建立日期的时候,建立日历也会变得更容易一些,由于咱们没必要把全部内容转换为秒以及从秒转换过来。 > > 例如,从新编写前面建立的一个表示明天日期的例子,使用以下的代码: > > NSDate *now = [NSDate date]; > NSDateComponents *comp = > -[NSDateComponents alloc] init]; > [comp setDay:01]; > NSCalendar *myCal = [[NSCalendar > -lloc] initWithCalendarIdentifier: > -SGregorianCalendar]; > NSDate *tomorrow = [myCal > -ateByAddingComponents:comp > -oDate:now options:0]; > 当咱们想要把可供人类阅读的日期和时间显示给用户,NSDate自己并非特别容易实现。所以,咱们一般会使用NSDateFormatter。 > > 使用NSDateFormatter得到表示当前日期的一个字符串,使用以下代码: > > NSDate *now = [NSDate date]; > NSDateFormatter *formatter = > -[NSDateFormatter alloc] init]; > [formatter setDateStyle: > -SDateFormatterMediumStyle]; > NSString *friendlyDate = > -formatter stringFromDate:now]; > 获取当前时间,可使用以下代码: > > NSDate *now = [NSDate date]; > NSDateFormatter *formatter = > -[NSDateFormatter alloc] init]; > [formatter setTimeStyle: > -SDateFormatterMediumStyle]; > NSString *friendlyTime = > -formatter stringFromDate:now]; > 表1-2给出了5种预约义的格式化样式。 > > 最后,也可使用一个日期格式的dateFormat属性来手动地设置样式: > > NSDate *now = [NSDate date]; > NSDateFormatter *formatter = > -[NSDateFormatter alloc] init]; > [formatter setDateFormat: > -"yyyy-mm-dd"]; > NSString *friendlyDate = > -formatter stringFromDate:now]; > 注意formatter的格式大小写必须严格按照文档提供的,不然会出现错误! >