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];

NSDatePicker && NSDate的更多相关文章

  1. iOS NSDate等时间类的使用

    一.NSDate NSDate对象用来表示一个具体的时间点. NSDate是一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体. NSDate存储的是GMT时间,使用的时候会根据 ...

  2. Foundation框架下的常用类:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver

    ========================== Foundation框架下的常用类 ========================== 一.[NSNumber] [注]像int.float.c ...

  3. 【原】iOS学习之NSDate在项目中的一些类目扩展

    在项目中,我们可能会面对各种各样的对于时间的需求,在这里提供几种可能会用到的需求代码 1.与今天的时间做比较,返回日期差值 代码: - (NSInteger)compareWithToday { NS ...

  4. 时间戳转化为日期TimeStamp转NSDate转NSString

    //时间戳处理 NSInteger time = [self.album.updatedAt integerValue] / 1000; NSNumber *timer = [NSNumber num ...

  5. 【Objective-C】NSDate详解及获取当前时间等常用操作

    NSDate类用于保存时间值,同时提供了一些方法来处理一些基于秒级别时差(Time Interval)运算和日期之间的早晚比较等. 1. 创建或初始化可用以下方法 用于创建NSDate实例的类方法有 ...

  6. iOS NSDate本地化

    1. NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; [outputFormatter setLocale:[NS ...

  7. NSDate和NSDateFormatter 相关应用代码示例

    此方法用来计算当前时间与目标时间的先后顺序: -(NSDate *)calculateTimeWithCurrentTime:(NSDate *)currentDate{ //将当前时间转为本地时区 ...

  8. OC — (Foundation框架-NSDate)

    NSDate:是OC中处理日期时间的一个类,可以用来表示时间 获取当前的时间 NSDate *d = [NSDate date]; 创建日期时间对象 NSLog输出是当前时间 格林时间 格式化显示时间 ...

  9. NSDate 时间

    NSDate *date=[NSDate date]; NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; formatter.date ...

随机推荐

  1. Win7 x64bit安装Oracle10g

    解决方案:   步骤一:在解压出的oracle文件夹中搜索refhost.xml文件,搜索结果出现2条符合条件文件,这两个文件均需要修改.   打开文件发现内容中有包含...5.0 6.0等系统说明, ...

  2. 如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理

    导读 Nginx 是最快和最强大的 Web 服务器之一,以其高性能和低资源占用率而闻名.它既可以被安装为一个独立的 Web 服务器,也可以安装成反向代理 Web 服务器.在这篇文章,我将讨论在安装了 ...

  3. iOS 不规则的ImageView

    http://blog.csdn.net/kevinpake/article/details/41205715 我们在做iOS开发的时候,往往需要实现不规则形状的头像,如: 那如何去实现? 通常图片都 ...

  4. 剑指Offer 旋转数组的最小数字

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转 ...

  5. 极客DIY:制作一个可以面部、自主规划路径及语音识别的无人机

    引言 现在大部分无人机厂商都会为第三方开发者提供无人机API接口,让他们更容易地开发无人机飞行控制应用程序,让无人机想怎么玩就怎么玩.有的API接口可以帮助开发者开发基于Web版的APP.手机APP甚 ...

  6. BZOJ 1485: [HNOI2009]有趣的数列

    Description 求长度为 \(2n\) 的序列.要求 1. \(a_1<a_3<a_5<...<a_{2n-1}\) . 2. \(a_2<a_4<a_6& ...

  7. IOI2015 Boxes

    Description 给出一个环形,n个点,每次只能访问k个点,求最短距离. Sol 贪心. CCF的题解. 首先只会最多走一趟环形,根据抽屉原理,如果一边不足k个才会到另一边,所以对于第二次以上的 ...

  8. echart字符云之添加点击事件

    // 路径配置 require.config({ paths : { echarts : 'jquery/echarts-2.2.7/build/dist' } }); // 使用EChart.js画 ...

  9. 9.2---机器人走方格(CC150)

    这题就是dp的一般题目 public static int countWays(int x, int y){ if( x < 0 || y < 0) return -1; int[][] ...

  10. django xadmin 插件(3) 列表视图新增自定义按钮

    效果图: 编辑按钮是默认的list_editable属性对应的插件(xadmin.plugins.editable) 放大按钮对应的是自定义插件. 自定义按钮源码: xplugin.py(保证能够直接 ...