iOS -- 获取当前时间,以及出现的错误

来源:互联网 发布:营收数据分析 ppt模板 编辑:程序博客网 时间:2024/05/22 10:54

获取系统当前的时间方法如下:

//获取当前时间

    NSDate *sendDate = [NSDatedate];

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]init];

    [dateFormatter setDateFormat:@"yyyyMMdd"];

    NSString *locationString = [dateFormatter stringFromDate:sendDate];

    NSLog(@"locationStringlocationString:%@",locationString);


在这里要注意的地方是,日期的格式一定要写正确,正确格式如下

yyyy-MM-dd HH:mm:ss


加入格式写错了会出现如下的错误:

YYYYMMdd这样的格式显示如下

locationStringlocationString:20161228日 会出现年份不正确的现象。

YYYYmmDD

locationStringlocationString:201642362日 

其他的格式也会显示错误的时间。




0 0