美文网首页iOS开发资料收集区
iOS中通过<sys/time.h>快速

iOS中通过<sys/time.h>快速

作者: 我在鄱阳湖边 | 来源:发表于2016-11-16 18:28 被阅读113次

    在iOS代码中,要想取得当前时间,最常见的方法是通过NSDate转化成NSString,得到可以使用的时间字符串

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

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];

    NSLog(@"%@", strDate);

    但是还有一个方法也可以获得当前时间,也是很方便的,但是要引入头文件<sys/time.h>

    struct timeval tv;

    gettimeofday(&tv, NULL);

    struct tm *tm = localtime(&tv.tv_sec);

    NSString *filename = [NSString stringWithFormat:@"%.4d%.2d%.2d%.2d%.2d%.2d.jpg", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec];

    就是这么简单

    相关文章

      网友评论

        本文标题: iOS中通过<sys/time.h>快速

        本文链接:https://www.haomeiwen.com/subject/ptvrpttx.html