首页 > 其他 > 详细

关于日期和时间戳的转换比较

时间:2017-03-25 10:39:05      阅读:295      评论:0      收藏:0      [点我收藏+]

#pragma mark - 将某个时间戳转化成 时间

-(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    

//    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

//    [formatter setTimeZone:timeZone];

    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];

    NSLog(@"1296035591  = %@",confromTimesp);

 

    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

    

    NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);

    return confromTimespStr;

}

#pragma  mark - 将字符串日期转换成想要格式的 NSDate

-(void)stringSwitchdate{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    NSDate* date = [formatter dateFromString:[self timestampSwitchTime:1490354236 andFormatter:@"YYYY-MM-dd HH:mm:ss"]]; //------------将字符串按formatter转成nsdate

    NSLog(@"%@",date);

}

 

#pragma mark - 两个日期的时间差 返回秒数

-(void)differentTime{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    NSDate *date1 = [formatter dateFromString:@"2017-03-24 19:42:30"];

    NSDate *date2 = [formatter dateFromString:@"2017-03-24 20:42:30"];

    //返回秒数

    NSTimeInterval aTimer = [date2 timeIntervalSinceDate:date1];

    

    int hour = (int)(aTimer/3600);

    int minute = (int)aTimer/60;

    NSString *dural = [NSString stringWithFormat:@"%d时%d分%f秒", hour, minute,aTimer];

    

    NSLog(@"%@",dural);

}

关于日期和时间戳的转换比较

原文:http://www.cnblogs.com/dujiahong/p/6616072.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!