zoukankan      html  css  js  c++  java
  • [oc学习日记]NSDate

    首先介绍一下返回日期对象的方法

     1         //获取当前日期时间
     2         NSDate *now = [NSDate date];
     3         NSLog(@"%@",now);
     4         //返回自从1970年之后经过多少秒(可正可负)之后的日期时间
     5         NSDate *mydate1970 = [NSDate dateWithTimeIntervalSince1970:60*60];
     6         NSLog(@"%@",mydate1970);
     7         //返回自从当前时间经过多少秒(可正可负)之后的日期时间
     8         NSDate *mydatenow = [NSDate dateWithTimeIntervalSinceNow:-60*60];
     9         NSLog(@"%@",mydatenow);
    10         //返回给定时间经过多少秒(可正可负)之后的日期时间
    11         NSDate *mydatedate = [now dateByAddingTimeInterval:60*60];
    12         NSLog(@"%@",mydatedate);
    13         //获取两个时间之中比较早的那一个
    14         NSDate *ear = [mydate1970 earlierDate:mydatenow];
    15         NSLog(@"%@",ear);
    16         //获取两个时间中比较晚的那一个
    17         NSDate *last = [mydate1970 laterDate:mydatenow];
    18         NSLog(@"%@",last);

    接下来介绍一下返回秒数的方法

    1         //返回当前时间自1970年之后经过的秒数
    2         NSTimeInterval mytime1970 = [[NSDate date]timeIntervalSince1970];
    3         NSLog(@"%f",mytime1970);
    4         //返回给定时间距离现在经过的秒数
    5         NSTimeInterval mytimenow = [mydate1970 timeIntervalSinceNow];
    6         NSLog(@"%f",mytimenow);
    7         //获取两个时间的时间差
    8         NSTimeInterval timedis = [mydate1970 timeIntervalSinceDate:mydatenow];
    9         NSLog(@"%f",timedis);

    接下来讲述两个 重点用法

    1.日期格式转化为字符串格式

    字符串格式按照格式化对象的设置进行输出 yyyy为年  也可写为yy之输出后两位  MM只能是月 如果只写一个m会省去前面的0 如果月份是两位数系统会自动加上缺省的那位

    a  为上下午  24小时制小时用H 12小时制用h

    系统会自动给你转化时差

    1         //日期时间格式化对象  格式化的同时将事件转化为当前时区的时间
    2         NSDateFormatter *dateformatter = [NSDateFormatter new];
    3         [dateformatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒 a vvvv"];
    4         NSString *mystedate = [dateformatter stringFromDate:now];
    5         NSLog(@"%@",mystedate);

    2.字符串转化为日期格式

    注意:转化出来的日期会有时差

    1         //日期格式的字符串转化为日期对象  格式化的内容要与字符串内容一样
    2         NSString *mystrdate = @"2012-12-12 10:12:59";
    3         NSDateFormatter *dateformatter1 = [NSDateFormatter new];
    4         [dateformatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    5         NSDate *mystrdd = [dateformatter1 dateFromString:mystrdate];
    6         NSLog(@"%@",mystrdd);

    接下来为大家介绍时区

    首先是用的不太多只需了解的内容

    1         //系统时区
    2         NSTimeZone *syszone = [NSTimeZone systemTimeZone];
    3         //本地时区
    4         NSTimeZone *localzone = [NSTimeZone localTimeZone];
    5         //默认时区
    6         NSTimeZone *defaultzone = [NSTimeZone defaultTimeZone];
    7         NSLog(@"%@",syszone);
    8         NSLog(@"%@",[localzone abbreviation]);
    9         NSLog(@"%@",[defaultzone abbreviation]);

    查看系统中收录了的时区

    1         //系统中所有时区的数组
    2         NSArray *zonearr = [NSTimeZone knownTimeZoneNames];
    3         NSLog(@"%@",zonearr);
    4         //系统中所有时区的字典
    5         NSDictionary *zonedic = [NSTimeZone abbreviationDictionary];
    6         NSLog(@"%@",zonedic);

    最后就是重中之重——————时差转换

    1.格林尼治时区转化

    1         //转换时差
    2         NSString *strdate = @"2015年6月6日 8时8分56秒";
    3         NSDateFormatter *formatter = [NSDateFormatter new];
    4         [formatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
    5         //设置时区
    6         [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    7         NSDate *date = [formatter dateFromString:strdate];
    8         NSLog(@"%@",date);

    2.设置时区

    1          //转换时差
    2          NSString *strdate = @"2015年6月6日 8时8分56秒";
    3          NSDateFormatter *formatter = [NSDateFormatter new];
    4          [formatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
    5          //两种方式都可以  最好用第一种 第二种有的时区会没有收录因而无法转换
    6          [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    7          [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    8          NSDate *date = [formatter dateFromString:strdate];
    9          NSLog(@"%@",date); 

    3.格林尼治时间差转换

     1         //计算系统时区和格林尼治的时间差  得到的是秒数
     2         NSInteger timeoff = [syszone secondsFromGMT];
     3         NSLog(@"%zi",timeoff);
     4         //转换时差
     5         NSString *strdate = @"2015年6月6日 8时8分56秒";
     6         NSDateFormatter *formatter = [NSDateFormatter new];
     7         [formatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
     8         //在当前时间加上时差得到正确时间
     9         NSDate *firdate = [formatter dateFromString:strdate];
    10         NSDate *date = [firdate dateByAddingTimeInterval:timeoff];
    11         NSLog(@"%@",date);
  • 相关阅读:
    第十一章 前端开发-jQuery
    第十一章 前端开发-JavaScript
    js特效 15个小demo
    开发”小米商城官网首页”(静态页面)
    浅析BFC及其作用
    python mysql curros.executemany 批量添加
    第十一章 前端开发-css
    spark读取文件机制 源码剖析
    zookeeper从入门到放弃(转载学习)
    hbase架构和读写过程
  • 原文地址:https://www.cnblogs.com/0error/p/4556346.html
Copyright © 2011-2022 走看看