zoukankan      html  css  js  c++  java
  • IOS> 获取当前系统时间

    1. NSDate + NSDateFormatter

        NSDate *date = [NSDate date];
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"YYYY/MM/dd hh:mm:ss.SSSSSSSS"];
        NSString *sDate = [format stringFromDate:date];
        NSLog(@"%@",sDate);
    

    执行结果:精确到1ms

    2015-03-30 21:35:13.799 My Legend[1037:53843] 2015/03/30 09:35:13.79900000
    

    2.NSDate + NSCalendar + NSDateComponents

        NSDate *now = [NSDate date];
        NSLog(@"now date is: %@", now);
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitNanosecond;
        NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
        NSLog(@"%ld/%ld/%ld %ld:%ld:%ld.%ld",(long)dateComponent.year, (long)dateComponent.month, (long)dateComponent.minute, (long)dateComponent.hour, (long)dateComponent.minute, (long)dateComponent.second, (long)dateComponent.nanosecond);
    

     
    执行结果:精确到1ns

    2015-03-30 21:35:13.800 My Legend[1037:53843] now date is: 2015-03-30 13:35:13 +0000
    
    2015-03-30 21:35:13.800 My Legend[1037:53843] 2015/3/35 21:35:13.800027012
    

    3.C标准库函数 time

    time_t 类型是用来存储从1970年到现在经过了多少秒

            time_t tBuf;
            tBuf=time(NULL);
            NSLog(@"%s", ctime(&tBuf));
            NSString *date = [NSString stringWithCString:ctime(&tBuf) encoding:NSUTF8StringEncoding];
            NSLog(@"%@\n", date);
    

     执行结果 精确到1s

    Sun Aug  2 11:42:01 2015
    
    Sun Aug  2 11:42:01 2015
    

    注: 可以用tm提取time时间信息

    struct tm {//tm结构
    	int	tm_sec;		/* seconds after the minute [0-60] */
    	int	tm_min;		/* minutes after the hour [0-59] */
    	int	tm_hour;	/* hours since midnight [0-23] */
    	int	tm_mday;	/* day of the month [1-31] */
    	int	tm_mon;		/* months since January [0-11] */
    	int	tm_year;	/* years since 1900 */
    	int	tm_wday;	/* days since Sunday [0-6] */
    	int	tm_yday;	/* days since January 1 [0-365] */
    	int	tm_isdst;	/* Daylight Savings Time flag */
    	long	tm_gmtoff;	/* offset from CUT in seconds */
    	char	*tm_zone;	/* timezone abbreviation */
    };
    
            //提取从time获取的 time_t类型 时间信息
            struct tm tmBuf;
            tmBuf=*localtime(&tBuf);
            NSLog(@"%d:%d:%d", tmBuf.tm_hour, tmBuf.tm_min,tmBuf.tm_sec);
    

    执行结果

    11:57:27
    
  • 相关阅读:
    (转)程序员应该知道的10个eclipse调试技巧
    Hibernate缓存
    【转】Hibernate 原汁原味的四种抓取策略
    hibernate 延迟加载和抓取策略
    移动端接口安全
    Thinkphp3.2.3中的RBAC权限验证
    单例模式--工厂模式
    php爬虫入门
    php爬虫入门
    .htaccess使用方法介绍
  • 原文地址:https://www.cnblogs.com/fortunely/p/4472172.html
Copyright © 2011-2022 走看看