Object C基础回顾

NSDate

2017-09-08  本文已影响0人  DVWang

NSDate * date = [[NSData alloc] init];
//NSData 时间类 主要用于时间数据的管理 存储默认的时间是格林 时间 因此使用时需要 主要时区的转换
//1.获取当前系统时间
NSDate * dateNow = [[NSDate alloc] init];
NSDate * dateNow1 = [NSDate date];

    //2.自定义打印时间格式
    //创建时间格式对象
    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    //选择系统提供的格式
    //[df setTimeStyle:<#(NSDateFormatterStyle)#>]
    
    //自定义设置格式
    [df setDateFormat:@"G YYYY-MM-dd EEE hh:mm:ss.SS"];
    //将时间对象dateNow 以df定义的格式 转换为字符串
    //ps:表示月的时候 月必须用'M' 表示分'm' 秒's' 毫秒'S' 其余皆可
    NSString * dateStr = [df stringFromDate:dateNow];
    NSDate * dateNew = [df dateFromString:@"2015-04-25 10:55:22.00"];
  
    NSLog(@"%@",dateStr);
    //3.常用方法
    //date 与当前系统时间的差
    //[date timeIntervalSinceNow];
    //date与1970的差
  //  [date timeIntervalSince1970];
    //date与dateNew的差
    //[date timeIntervalSinceDate:dateNew];
    
    //NSTimeInterval 实际就是double类型 主要用于表示时间的差值 相差的秒数 用%lf 打印
   // NSTimeInterval time = [date timeIntervalSinceDate:dateNew];

//两种方式
//第一种 使用dateFormatter 输入字符串 根据格式 输出时间
NSString * string = @"2015-03 20, 03:20:20";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"yyyy-MM dd, HH:mm:ss";
//格式写好了 转换成 NSDate
NSDate* date = [dateFormatter dateFromString:string];
NSLog(@"%@",date);

    //第二种方式
    NSDateComponents *components = [[NSDateComponents alloc]init];
    //day表示天
    components.day = 20;
    //month表示月份
    components.month =2;
    //year表示年
    components.year =2015;
    //hour表示时
    components.hour =12;
    //minute表示分
    components.minute = 23;
    //second表示秒
    components.second = 20;

//部件写好之后 要合成NSDate 使用 日历
    NSCalendar * calender = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate * date2 = [calender dateFromComponents:components];
    NSLog(@"%@",date2);
    
    NSLocale * locale = [NSLocale currentLocale];
    NSString * time = [date2 descriptionWithLocale:locale];
    NSLog(@"%@",time);
    
 //NSTimeZone 倒换时区时候用的类

//创建 一个时间 默认为当前时间 获取的是 格林威治时间
NSDate * currentDate = [NSDate date];
//获取当前系统环境下的
NSLocale locale = [NSLocale currentLocale];
//把当前系统下获取的值 转换字符串来打
//如果直接locale调用description
NSString * time = [currentDate descriptionWithLocale:locale];
//想要获取一个以当前时间为基础的时间 60
60
NSDate* futureTime = [NSDate dateWithTimeIntervalSinceNow:3600];
//获取一个小时以前的时间
NSDate * pastTime = [NSDate dateWithTimeIntervalSinceNow:-3600];
//如果想获取一个过去很久的时间 到开始纪元的时候
NSDate * longlongAgo = [NSDate distantPast];
NSDate * longlongFuture = [NSDate distantFuture];
//做一个手表的 跟定时器(NSTimer) 一起来使用的
//比较俩个日期的 谁早 睡晚
NSDate * earlyDate = [currentDate earlierDate:futureTime];
NSDate * laterDate = [currentDate laterDate:futureTime];
//比较俩个时间是否相等 返回值是布尔类型
BOOL ret= [earlyDate isEqualToDate:laterDate];

 //创建  dateFormatter 规定转换 格式的
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    //系统自带了一些格式
    dateFormatter.dateStyle =NSDateFormatterMediumStyle;
    NSString * string= [dateFormatter stringFromDate:date];
    NSLog(@"%@",string);
    //自定义格式
    dateFormatter.dateFormat =@"yyyy:MM:dd HH:mm:ss";
    NSString * string1 = [dateFormatter stringFromDate:date];
上一篇下一篇

猜你喜欢

热点阅读