IOS 时间和时间戳之间转化示例
以毫秒为整数值的时间戳转换
时间戳转化为时间NSDate
-(NSString*)timeWithTimeIntervalString:(NSString*)timeString
{
//格式化时间
NSDateFormatter*formatter=[[NSDateFormatteralloc]init];
formatter.timeZone=[NSTimeZonetimeZoneWithName:@"shanghai"];
[formattersetDateStyle:NSDateFormatterMediumStyle];
[formattersetTimeStyle:NSDateFormatterShortStyle];
[formattersetDateFormat:@"yyyy年MM月dd日HH:mm"];
//毫秒值转化为秒
NSDate*date=[NSDatedateWithTimeIntervalSince1970:[timeStringdoubleValue]/1000.0];
NSString*dateString=[formatterstringFromDate:date];
returndateString;
}
时间转化为时间戳
//当前时间 NSDate*date=[NSDatedateWithTimeIntervalSinceNow:0]; NSTimeIntervala=[datetimeIntervalSince1970]*1000;//*1000是精确到毫秒,不乘就是精确到秒 NSString*timeString=[NSStringstringWithFormat:@"%.0f",a];//转为字符型
通过比较时间与当前时间返回年月日的方法
-(void)getBabyDetailAge:(NSString*)date
{
//获得日期对象
NSDateFormatter*formatter_=[[NSDateFormatteralloc]init];
formatter_.dateFormat=@"yyyy-MM-ddHH:mm:ss";
NSDate*createDate=[formatter_dateFromString:date];
NSCalendar*gregorian=[[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSUIntegerunitFlags=NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear;
NSDateComponents*components=[gregoriancomponents:unitFlagsfromDate:createDatetoDate:[NSDatedate]options:0];
NSIntegeryears=[componentsyear];
NSIntegermonths=[componentsmonth];
NSIntegerdays=[componentsday];
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下