OC - 时间日期类NSDate

来源:互联网 发布:文华财经随身行mac 编辑:程序博客网 时间:2024/05/19 14:51
   //NSDate 时间日期类 NSDate 二进制数据流        {            //1.获取当前时间 零时区的时间            //显示的是格林尼治的时间: 年-月-日 时:分:秒:+时区            NSDate *date = [NSDate date];            NSLog(@"当前零时区时间 %@", date);            //2.获得本地时间 东八区 晚八个小时 以秒计时            NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:8 * 60 * 60];            NSLog(@"今天此时的时间 %@",date1);            //3.昨天此时的时间            NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:(-24 + 8) * 60 * 60];            NSLog(@"昨天此时的时间 %@",yesterdayDate);            //4.明天此刻            NSDate *tomorrowDate = [NSDate dateWithTimeInterval:24 * 60 * 60 sinceDate:date1];            NSLog(@"明天此刻的时间 %@",tomorrowDate);            //5.NSTimeInterval 时间间隔(单位是秒),double 的 typedef            //昨天此时与明天此刻的时间间隔            NSTimeInterval timeInterval = [tomorrowDate timeIntervalSinceDate:yesterdayDate];            NSLog(@"昨日和明天此刻的时间(秒) %.0f",timeInterval);            //练习: 计算一个当前时间和一个固定时间的差值如果差值在60妙以内输出“刚刚”,如果在60秒到3600之前,则输出“XX分钟之前”,若在3600到24 *3600 之内,则输出“XX小时之前”,若再24 * 3600之外,则显示固定时间            {                //保证两个日期是在同一个时区                NSDate *date = [NSDate dateWithTimeIntervalSinceNow:  4000];                NSDate *now = [NSDate date];                NSTimeInterval timeInterval = [now timeIntervalSinceDate:date];                if (timeInterval < 60 && timeInterval > 0) {                    NSLog(@"刚刚");                }                else if(timeInterval >= 60 && timeInterval <= 3600){                    NSLog(@"%.0f分钟前",timeInterval / 60);                }                else if(timeInterval > 3600){                    NSLog(@"%.0f小时前", timeInterval / 3600);                }                else{                    NSLog(@"在当前时间之后");                }            }            //时间戳:从1970.1.1 到当前时间的时间间隔就叫时间戳            //常见于网址中 1970 1月1日0时0分0秒 计算机元年            //32位系统 能够表示的最大数 2^31 - 1 表示时间的最大时间是68.1年, 也就是2038年2年份左右, 64位系统能够表示2924亿年            {                NSTimeInterval timeIn = [date timeIntervalSince1970];                NSLog(@"1970年1月1日0时0分0秒至今相差 %.0f 秒", timeIn);            }        }//        NSDateFormatter NSDate和字符串的相互转换        {            //一、date转字符串            {                //系统会认为字符串是零时区的时间, 转成NSDate是东八时区的                //例子:将当前日期转成字符串(两种格式)                //设定日期格式:yyyy(年) - MM(月) - dd(日)  H(小时) m (分钟)s(秒)                NSDate *date = [NSDate date];                //创建一个格式对象                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];                //2015 - 12 - 08 11:01:01   hh: 12小时;HH: 24小时                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];                NSString *dateStr = [dateFormatter stringFromDate:date];                NSLog(@"字符串表示:%@",dateStr);                //2015年12年08月 11时01分01秒 YY:12 yyyy:2012                NSDateFormatter *dateFormaterA = [[NSDateFormatter alloc]init];                [dateFormaterA setDateFormat:@"yyyy年MM年dd日 HH时mm分ss秒"];                NSString *dateStrA = [dateFormaterA stringFromDate:date];                NSLog(@"%@",dateStrA);            }            //二、字符串转date            {                //系统会认为字符串是东八区的时间, 转乘NSDate是零时区的                NSString *dateStr = @"2011年11月11日 11时11分11秒";                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];                [dateFormatter setDateFormat:@"yyyy年MM月dd日 hh时mm分ss秒"];                NSDate *date = [dateFormatter dateFromString:dateStr];                NSDate *date2 = [date dateByAddingTimeInterval:8 * 60 * 60];//将转换回来的对象手动加上8小时,回到北京时间                NSLog(@"字符串转data: %@",date2);            }        }
0 0
原创粉丝点击