<OC>NSDate相关

  日期格式转换方法

  

//日期转换方法
-(NSString *)formatTimeStr:(NSString *)timeStr
{
    NSDate *Date;
    //新建一个Date格式类,
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置为timeStr的日期格式
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
    //以timeStr的格式来得到Date
    Date = [dateFormatter dateFromString:timeStr];
    //设置日期格式为要转化的类型
    [dateFormatter setDateFormat:@"yyyyMMdd HHmmss"];
    //将要转化的日期变为字符串
    NSString *formatStr = [dateFormatter stringFromDate:Date];
    [dateFormatter release];
    return formatStr;
}

  [NSDate date]获取的是GMT时间,下面方法获得某个时区的时间:

-(NSString *)getDate
{
    //[NSDate date]获取的是GMT时间,不是当前时区的时间
    NSDate *date = [NSDate date];
    //从系统当前时区新建一个时区
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    //从GMT时间得到当前时区的总的秒数
    NSInteger interval = [zone secondsFromGMTForDate:date];
    //从上面得到的秒数获得当前时区的时间
    NSDate *localDate = [date dateByAddingTimeInterval:interval];
    //以特定格式得到当前时间的字符串
    NSDateFormatter *form = [[NSDateFormatter alloc] init];
    //y年M月d日 H时m分s秒(注意M和m)
    [form setDateFormat:@"yyyy-MM-dd"];
    NSString *str = [form stringFromDate:localDate];
    [form release];
    return [str retain];
}

   NSDate的其他常用代码:

  用于创建实例的方法:

NSDate常用方法(创建实例的方法)
//返回当前时间
+(id)date;
//返回以当前时间为基准,然后过了secs秒的时间
+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; 
//返回以2001/01/01 GMT为基准,然后过了secs秒的时间
+ (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;
//返回以1970/01/01 GMT为基准,然后过了secs秒的时间
+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
//返回很多年以后的未来的某一天。
    //比如你需要一个比现在(Now)晚(大)很长时间的时间值,则可以调用该方法。测试返回了4000/12/31 16:00:00
+ (id)distantFuture;
//返回很多年以前的某一天。
    //比如你需要一个比现在(Now)早(小)大很长时间的时间值,则可以调用该方法。测试返回了公元前0001/12/31 17:00:00
+ (id)distantPast;

//初始化为当前时间。类似date方法
- (id)init;
//初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。类似dateWithTimeIntervalSinceReferenceDate:方法
 - (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;
//初始化为以refDate为基准,然后过了secs秒的时间
 - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;
//初始化为以当前时间为基准,然后过了secs秒的时间
- (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;

  日期间比较的方法:

用于比较的方法
- (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

  取回时间间隔的方法:

View Code
    - (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的时间间隔

  description方法

description方法
- (NSString *)description;
    以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。
    其中 "±HHMM" 表示与GMT的存在多少小时多少分钟的时区差异。比如,若时区设置在北京,则 "±HHMM" 显示为 "+0800"

 

 
posted @ 2012-11-09 10:58  白条围巾  阅读(422)  评论(0编辑  收藏  举报