NSDate与时间戳的那点事

来源:互联网 发布:为什么python安装失败 编辑:程序博客网 时间:2024/05/18 03:14

对于项目中经常使用的时间来说,通过时间戳的形式进行数据的操作能带来极大的方便,下面就时间戳的生成和转换通过Demo的形式进行讲解

声明一个时间类型的变量:

       // 获取当前的时间        // 下面的第一个方法不提倡        // NSDate *now1 = [[NSDate alloc]initWithTimeIntervalSinceNow:8*60*60];        NSDate * today = [NSDate date];        NSTimeZone *zone = [NSTimeZone systemTimeZone];        NSInteger interval = [zone secondsFromGMTForDate:today];        NSDate *localeDate = [today dateByAddingTimeInterval:interval];        NSLog(@"%@", localeDate);        // 时间转换成时间戳        NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)[localeDate timeIntervalSince1970]];        NSLog(@"timeSp : %@", timeSp);

时间戳转换成时间类型(NSDate)

        // 时间戳转换成日期        NSDate *currentTime = [NSDate dateWithTimeIntervalSince1970:[timeSp intValue]];        NSLog(@"currentTime : %@", currentTime);



0 0
原创粉丝点击