日期,时间的获取(ios)

大家一起学习,一起进步,有错的帮我提下,我会改的!

//路径

    NSBundle *strBundle = [NSBundle mainBundle];

    NSLog(@"-----路径比较  %@",strBundle);

    NSString *str = NSHomeDirectory();

   NSLog(@"-----路径二 %@",str);

    //当前日期

    NSDate *date = [NSDate date];

    NSLog(@"-----日期%@",date);

    //当前时间加上一定时间间隔

    NSTimeInterval secondsPerDay = 2*24*60*60;

    NSDate *tomorrow = [NSDatedateWithTimeIntervalSinceNow:secondsPerDay];

    NSLog(@"myDate = %@",tomorrow);

   

   

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSLog(@"----当月日历--%@",calendar);

    //unsigned已经过期

//    unsigned units  =NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;

    NSCalendarUnitunits=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond ;

 

    NSDateComponents *comp1 = [calendar components:unitsfromDate:date];

    NSInteger month = [comp1 month];

    NSInteger year = [comp1 year];

    NSInteger day = [comp1 day];

   

    NSInteger hour = [comp1 hour];

    NSInteger second =[comp1 second];

    NSInteger minute = [comp1 minute];

 

 

    NSLog(@"年:%zi,月:%zi,日:%zi",year,month,day);

    NSLog(@"时:%zi,分:%zi,秒:%zi",hour,minute,second);

    //NSDateFormatter实现日期的输出

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

    [formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码

    //或者是手动设置样式[formattersetDateFormat:@"yyyy-mm-dd"];

 

    NSString *string = [formatter stringFromDate:date];

NSLog(@"string = %@",string);

 

打印结果:

路径差别在前者的一个是bundle目录下的APPlication (应用里的主要图片,音频资源,xib文件)

本地图片,请重新上传

后者是data目录下的APPlication

本地图片,请重新上传

归档时候的文件在Preferences目录下

本地图片,请重新上传

2015-01-30 22:19:33.645 fjds[2293:208496] -----路径比较  NSBundle </Users/laiyufang/Library/Developer/CoreSimulator/Devices/493E5A96-8066-4EFD-8F69-60F9373DF5E5/data/Containers/Bundle/Application/1892D8BE-1AE4-4353-A27E-82892F9D6C31/fjds.app> (loaded)

2015-01-30 22:19:33.646 fjds[2293:208496] -----路径二  /Users/laiyufang/Library/Developer/CoreSimulator/Devices/493E5A96-8066-4EFD-8F69-60F9373DF5E5/data/Containers/Data/Application/D5B2F5CE-5C36-4922-865B-EFBE0656B15F

2015-01-30 22:19:33.647 fjds[2293:208496] -----日期2015-01-30 14:19:33 +0000

2015-01-30 22:19:33.648 fjds[2293:208496] myDate = 2015-02-01 14:19:33 +0000

2015-01-30 22:19:33.648 fjds[2293:208496] ----当月日历--<_NSCopyOnWriteCalendarWrapper: 0x7fde88cbd460>

2015-01-30 22:19:33.648 fjds[2293:208496] 年:2015,月:1,日:30

2015-01-30 22:19:33.648 fjds[2293:208496] 时:22,分:19,秒:33

2015-01-30 22:19:33.648 fjds[2293:208496] string = Friday, January 30, 2015


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值