获取当前日期为DD

获取当前日期为DD

本文介绍了iOS 8 - 获取当前日期为DD / MM / YYYY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  NSDateComponents * components = [[NSCalendarcurrentCalendar]组件:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYearfromDate:[NSDatedate]]; 

NSString * day = [components day];
NSString * week = [组件月];
NSString * year = [components year];

NSString * date = [NSString stringWithFormat:@%@。%@。%@,日,周,年];

^^我的代码不工作:S



是否有一种方式可以在1周内获得明天的日期,等等...



感谢:)

解决方案

您可以使用代码检索数字组件的变体,使用 NSCalendar 组件方法:

  NSDateComponents * components = [[NSCalendar currentCalendar] :NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; 

NSInteger day = [components day];
NSInteger月= [组件月];
NSInteger year = [components year];

NSString * string = [NSString stringWithFormat:@%ld。%ld。%ld,(long)day,(long)month,(long)year]

注意,这是组件,而不是code>组件。



或者更好的是,您可以使用 NSDateFormatter

  NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; 
formatter.dateFormat = @d.M.yyyy;
NSString * string = [formatter stringFromDate:[NSDate date]];


Can anybody give me some code how I get the date?

NSDateComponents *components = [[NSCalendarcurrentCalendar] component:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYearfromDate:[NSDatedate]];

NSString *day = [components day];
NSString *week = [components month];
NSString *year = [components year];

NSString *date = [NSString stringWithFormat:@"%@.%@.%@",day,week,year];

^^ my code is not working :S

And is there a way that I can get the date of tomorrow, in 1 week and so on...

Thanks :)

解决方案

You can either use a variation of your code that retrieves the numeric components using NSCalendar with the components method:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];

NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

NSString *string = [NSString stringWithFormat:@"%ld.%ld.%ld", (long)day, (long)month, (long)year];

Note, that's components, not component.

Or, better, you can use NSDateFormatter:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"d.M.yyyy";
NSString *string = [formatter stringFromDate:[NSDate date]];

这篇关于iOS 8 - 获取当前日期为DD / MM / YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 22:56