NSSting 与 NSDate 时间戳 恩怨情仇

NSSting 转 NSDate

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSDate *date = [dateFormatter dateFromString:resultString];

NSDate 转 NSString

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];

获取本地 NSDate

    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate: date];
    NSDate *localeDate = [date dateByAddingTimeInterval: interval];

时区转换

    NSDateFormatter *Formatter = [[NSDateFormatter alloc] init];
    [Formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [Formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    NSString *strDate = [Formatter stringFromDate:localeDate];
    NSLog(@"%@", strDate);

NSDate 转时间戳

NSDate *date = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];

时间戳转 NSDate

NSDate *date = [NSDate date];
NSTimeInterval  timeInterval = [date timeIntervalSince1970]-1800;//前30分钟
NSDate *newdate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS开发中,经常会遇到各种各样的时间问题,8小时时差,时间戳,求时间间隔,农历等等。解决办法网上比比皆是,但大多...
    小李龍彪阅读 11,581评论 1 6
  • 在iOS开发中,经常会遇到各种各样的时间问题,8小时时差,时间戳,求时间间隔,农历等等。解决办法网上比比皆是,但大...
    真巧了_嘿阅读 7,782评论 0 7
  • pragma mark NSDate:获取时间的一个类 //// 获取当前时间:获取到的date对象是零时区的时...
    向日葵_wwx阅读 11,390评论 3 4
  • 历时小半年,断断续续,各种事情,经历了各种找工作,找房子,终于,把《南方有令秧》看完了。此处应该有掌声~~~ 读书...
    南星YONA阅读 3,077评论 0 0