首页 > 移动平台 > 详细

iOS时间以yyyy-MM-dd格式输出的两种方法代码

时间:2014-03-14 16:24:38      阅读:364      评论:0      收藏:0      [点我收藏+]

//方法一

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

        [formatter setDateFormat:@"yyyy-MM-dd"];

        //    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

        NSString * locationString=[formatter stringFromDate:[NSDate date]];

        NSLog(@"今天日期是:%@",locationString);

        

        //方法二:

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

        NSDate *now;

        NSDateComponents *comps = [[NSDateComponentsalloc] init];

        NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |

        NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

        now=[NSDate date];

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

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

        int year=[comps year];

        int week = [comps weekday];

        int month = [comps month];

        int day = [comps day];

        int hour = [comps hour];

        int min = [comps minute];

        int sec = [comps second];

        NSLog(@"year=%d,week=%d,month=%d,day=%d,hour=%d,min=%d,sec=%d",year,week,month,day,hour,min,sec);

        NSLog(@"今天日期是:%d-%d-%d",year,month,day);

iOS时间以yyyy-MM-dd格式输出的两种方法代码,布布扣,bubuko.com

iOS时间以yyyy-MM-dd格式输出的两种方法代码

原文:http://www.cnblogs.com/tanwen1989/p/3600506.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!