Object-C 如何把一个时间戳转换为一个标准的时间格式?

这个原理是很是简单的,首先将获得的时间戳字段转换为浮点类型的数,计算出这个时间距离1970年的NSDate,而后设置显示的时间格式,转化一下便可,封装了一下代码,具体以下:code

+(NSString *)dateStringFromNumberTimer:(NSString *)timerStr{
    //转化为Double
    double time=[timerStr doubleValue];
    //计算距离1970年的NSDate
    NSDate *date=[NSDate dateEithTimeIntervalSince1970:time];
    //转化为时间格式字符串
    NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    df.dateFormat=@"yyyy-MM-dd HH:mm:ss";
    //转化为 时间字符串
    return [df stringFormDate:date];
}
相关文章
相关标签/搜索