码迷,mamicode.com
首页 > 其他好文 > 详细

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

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

标签:mat   val   orm   return   日期   oid   name   jin   sel   

#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);

}

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

标签:mat   val   orm   return   日期   oid   name   jin   sel   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!