开发中我们有的时候会对日期时间进行大小比较,时间格式化,时间的年份,月份,天进行处理,关于获取NSDate的的年月日目前有两种解决方式:
1.NSDateFormat获取
NSDate *date =[NSDate date];//简书 FlyElephant
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy"];
NSInteger currentYear=[[formatter stringFromDate:date] integerValue];
[formatter setDateFormat:@"MM"];
NSInteger currentMonth=[[formatter stringFromDate:date]integerValue];
[formatter setDateFormat:@"dd"];
NSInteger currentDay=[[formatter stringFromDate:date] integerValue];
NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,currentYear,currentMonth,currentDay);
2.NSDateComponents获取
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
NSInteger year=[components year];
NSInteger month=[components month];
NSInteger day=[components day];
NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",currentDate,year,month,day);
以上两种方式都能轻松获取年月日,iOS默认的NSDate是格林尼治时间,比中国时区的时间少8个小时,处理过日期的都知道8个小时的误差存在,我们在获取年月日的时候不要加上8个小时,iOS系统会自动帮我们自动加上八个小时,以下是个人测试代码:
//简书 FlyElephant
NSString *dateString = @"2016-03-31 23:59:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *mydate=[formatter dateFromString:dateString];
[formatter setDateFormat:@"yyyy"];
NSInteger currentYear=[[formatter stringFromDate:mydate] integerValue];
[formatter setDateFormat:@"MM"];
NSInteger currentMonth=[[formatter stringFromDate:mydate]integerValue];
[formatter setDateFormat:@"dd"];
NSInteger currentDay=[[formatter stringFromDate:mydate] integerValue];
NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",mydate,currentYear,currentMonth,currentDay);
NSString *dateStr= @"2016-04-01 00:01:00";
NSDateFormatter *nextformatter = [[NSDateFormatter alloc] init] ;
[nextformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[nextformatter dateFromString:dateStr];
[nextformatter setDateFormat:@"yyyy"];
NSInteger year=[[nextformatter stringFromDate:date] integerValue];
[nextformatter setDateFormat:@"MM"];
NSInteger month=[[nextformatter stringFromDate:date]integerValue];
[nextformatter setDateFormat:@"dd"];
NSInteger day=[[nextformatter stringFromDate:date] integerValue];
NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,year,month,day);
最终输出的结果:
<pre><code>
currentDate = 2016-03-31 15:59:00 +0000 ,year = 2016 ,month=3, day=31
</code></pre>
<pre><code>
currentDate = 2016-03-31 16:01:00 +0000 ,year = 2016 ,month=4, day=1</code></pre>
NSDate对象存放的日期始终是UTC的标准时间,如果想获取本地时间可以根据时间偏移量计算,不过最终的时区还是GMT时区.
<pre><code>`- (NSDate *)getCurrentLocalDate:(NSDate )date {
NSTimeZone sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
NSTimeZone *destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate *destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:date];
return destinationDateNow;
}`</code></pre>
网友评论