zoukankan      html  css  js  c++  java
  • NSDatePicker && NSDate

        UIDatePicker *datePicker = [[UIDatePicker alloc]init];

          datePicker.datePickerMode = UIDatePickerModeDate;

          datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];

          [datePicker addTarget:self action:@selector(datePicker:) forControlEvents:UIControlEventValueChanged];

             [self.view addSubview:datePicker];

     

    设置一个时间

        NSDateComponents *comps = [[NSDateComponents alloc]init];

        [comps setMonth:01];

        [comps setDay:00];

        [comps setYear:1850];

        NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

        NSDate *minDate = [calendar dateFromComponents:comps];

    NSDate常用方法

     

    NSDate类用于保存时间值,同时提供了一些方法来处理一些基于秒级别时差(Time Interval)运算和日期之间的早晚比较等。

     

    1. 创建或初始化可用以下方法

        用于创建NSDate实例的类方法有

        + (id)date;

        返回当前时间

     

        + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;   

        返回以当前时间为基准,然后过了secs秒的时间

     

        + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;

        返回以2001/01/01 GMT为基准,然后过了secs秒的时间

     

        + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

        返回以1970/01/01 GMT为基准,然后过了secs秒的时间

     

        + (id)distantFuture;

        返回很多年以后的未来的某一天。

        比如你需要一个比现在(Now)晚(大)很长时间的时间值,则可以调用该方法。测试返回了4000/12/31 16:00:00

     

        + (id)distantPast;

        返回很多年以前的某一天。

        比如你需要一个比现在(Now)早(小)大很长时间的时间值,则可以调用该方法。测试返回了公元前0001/12/31 17:00:00

     

        用于创建NSDate实例的实例方法有

        - (id)addTimeInterval:(NSTimeInterval)secs;

        返回以目前的实例中保存的时间为基准,然后过了secs秒的时间

     

        用于初始化NSDate实例的实例方法有

        - (id)init;

        初始化为当前时间。类似date方法

     

        - (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;

        初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。类似dateWithTimeIntervalSinceReferenceDate:方法

     

        - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;

        初始化为以refDate为基准,然后过了secs秒的时间

     

        - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;

        初始化为以当前时间为基准,然后过了secs秒的时间

     

     

    2. 日期之间比较可用以下方法

        - (BOOL)isEqualToDate:(NSDate *)otherDate;

        与otherDate比较,相同返回YES

     

        - (NSDate *)earlierDate:(NSDate *)anotherDate;

        与anotherDate比较,返回较早的那个日期

     

        - (NSDate *)laterDate:(NSDate *)anotherDate;

        与anotherDate比较,返回较晚的那个日期

     

        - (NSComparisonResult)compare:(NSDate *)other;

        该方法用于排序时调用:

          . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame

          . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

          . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

     

     

    3. 取回时间间隔可用以下方法

        - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;

        以refDate为基准时间,返回实例保存的时间与refDate的时间间隔

     

        - (NSTimeInterval)timeIntervalSinceNow;

        以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔

     

        - (NSTimeInterval)timeIntervalSince1970;

        以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔

     

        - (NSTimeInterval)timeIntervalSinceReferenceDate;

        以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔

     

     

        + (NSTimeInterval)timeIntervalSinceReferenceDate;

        以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔

     

     

    4. 将时间表示成字符串

        - (NSString *)description;

        以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。

        其中 "±HHMM" 表示与GMT的存在多少小时多少分钟的时区差异。比如,若时区设置在北京,则 "±HHMM" 显示为 "+0800"

     

    //得到当前的日期

     NSDate *date = [NSDate date];

     NSLog(@"date:%@",date);

     

     //得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:

     NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];

     NSLog(@"yesterday:%@",yesterday);

     

    NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];

     NSDate *date = [NSDate date];

     [formatter setTimeStyle:NSDateFormatterMediumStyle];

     NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

     NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];

     NSInteger unitFlags = NSYearCalendarUnit | 

                           NSMonthCalendarUnit |

                           NSDayCalendarUnit | 

                           NSWeekdayCalendarUnit | 

                           NSHourCalendarUnit |

                           NSMinuteCalendarUnit |

                           NSSecondCalendarUnit;

     //int week=0;

     comps = [calendar components:unitFlags fromDate:date];

     int week = [comps weekday]; 

     int year=[comps year]; 

     int month = [comps month];

     int day = [comps day];

     //[formatter setDateStyle:NSDateFormatterMediumStyle];

     //This sets the label with the updated time.

     int hour = [comps hour];

     int min = [comps minute];

     int sec = [comps second];

     NSLog(@"week%d",week);

     NSLog(@"year%d",year);

     NSLog(@"month%d",month);

     NSLog(@"day%d",day);

     NSLog(@"hour%d",hour);

     NSLog(@"min%d",min);

     NSLog(@"sec%d",sec);

    //得到毫秒

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

     [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

     [dateFormatter setTimeStyle:NSDateFormatterShortStyle];

     //[dateFormatter setDateFormat:@"hh:mm:ss"]

     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];

     NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);

     [dateFormatter release];

     

  • 相关阅读:
    vue create is a Vue CLI 3 only command and you are using Vue CLI 2.9.6. You
    Vue2.x是怎么收集依赖的
    只绑定一次事件的简单方法
    Proxy是怎么做数据劫持的
    使用babel进行打包
    使用npm link进行模块调试
    Webpack 热加载插件的实现原理
    Vue 服务端渲染的数据流
    Vue的生命周期钩子
    Linux定时任务
  • 原文地址:https://www.cnblogs.com/shifu/p/5498190.html
Copyright © 2011-2022 走看看