NSDate和NSString之间的转换

NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间

好像是从ios4.1开始[NSDate date];获取的是GMT时间,这个时间和北京时间相差8个小时,以下代码可以解决这个问题

//方法一

- (void)tDate

{

    NSDate *date = [NSDatedate];

    NSTimeZone *zone = [NSTimeZonesystemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: date];

    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];  

    NSLog(@"%@", localeDate);

}

方法二

- (NSString*)dateAsString:(NSDate*)date

 {

/* Create a single string expressing a mountain's climbed date, properly localized */

NSString *returnValue = @"";

    NSDateFormatter *dateFormatter = nil;

if (date != nil) {

if (dateFormatter ==nil) {

dateFormatter = [[NSDateFormatteralloc]init];

}

//原文地址:http://blog.csdn.net/diyagoanyhacker/article/details/7096612

//作者:禚来强

[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

[dateFormatter setLocale:[NSLocalecurrentLocale]];

returnValue = [dateFormatter stringFromDate:date];

}

/* As this code uses the current "locale", the date format will be in the format 

     specified by the user's "Region Format" settings.  If you need to use an

     alternate format internally, you can create and use NSLocales, e.g.:

 

     NSLocale *enGBLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

     [numberFormatter setLocale:enGBLocale];

 

     Doing so will not affect the current user-set language or region format. 

     

     Similarly, while you should always rely on the system and application bundle

     to pick the most appropriate resources for the current user language setting,

     if you need to know what the current user language setting is, you can do

     something like the following:

 

     NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];

     NSArray* languages = [defs objectForKey:@"AppleLanguages"];

     NSString* preferredLang = [languages objectAtIndex:0];

     NSLog(@"Current language is %@", preferredLang);

 

     Note that the iPhone does not support locales in the same way that Mac OS

     does (really only using locales for the Region Format settings) so if you

     try and get an array from standardUserDefaults for the key "AppleLocale",

     this will fail on the iPhone. */

return returnValue;

}


//方法三

+ (NSString *)fixStringForDate:(NSDate *)date 

{

    NSDateFormatter* dateFormatter = [[NSDateFormatteralloc]init];

    [dateFormatter setDateStyle:kCFDateFormatterFullStyle];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *fixString = [dateFormatter stringFromDate:date]; 

    [dateFormatter release];

    return fixString;

}


方法四

    NSDateComponents *comps = [[NSDateComponentsalloc]init];

    [comps setYear:2010];

    [comps setMonth:8];

    [comps setDay:24];

    [comps setHour:17];

    [comps setMinute:5];

    [comps setTimeZone: [NSTimeZonetimeZoneWithAbbreviation:@"UTC"]];

    NSLog(@"%@", [NSTimeZonetimeZoneWithAbbreviation:@"UTC"]);

    

    NSCalendar *cal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *referenceTime = [cal dateFromComponents:comps];

    NSLog(@"%@", referenceTime);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值