时间戳转换(10位、13位)

转载请注明转自:http://blog.csdn.net/lea__dongyang


一、10位时间戳转换成字符串
+(NSString *)getDateString:(NSString *)spString
{
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"HH:mm"];
NSString* string=[dateFormat stringFromDate:confromTimesp];
return string;
}
二、13位时间戳转化为时间
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
// 格式化时间
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];

// 毫秒值转化为秒
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
NSString* dateString = [formatter stringFromDate:date];
return dateString;
}

三、时间转化为时间戳(13位)

// 当前时间
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒
NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型

四、通过比较时间与当前时间返回年月日的方法
- (void)getBabyDetailAge:(NSString *)date
{
// 获得日期对象
NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init];
formatter_.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *createDate = [formatter_ dateFromString:date];

NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian];
NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
NSDateComponents *components = [gregorian components:unitFlags fromDate:createDate toDate:[NSDate date] options: 0 ];

NSInteger years = [components year];
NSInteger months = [components month ];
NSInteger days = [components day ];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值