iOS 8 - 获取当前日期为 DD/MM/YYYY

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27790657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:14:40  来源:igfitidea点击:

iOS 8 - get current date as DD/MM/YYYY

iosdatensdate

提问by Tim

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

^^ 我的代码不起作用:S

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

有没有一种方法可以让我获得明天的日期,在 1 周内等等......

Thanks :)

谢谢 :)

回答by Rob

You can either use a variation of your code that retrieves the numeric components using NSCalendarwith the componentsmethod:

您可以使用代码的变体来检索数字组件NSCalendarcomponents方法是使用:

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.

请注意,那是components,不是component

Or, better, you can use NSDateFormatter:

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

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

回答by Adarsh G J

  NSDate *todayDate = [NSDate date]; //Get todays date 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date. 
    [dateFormatter setDateFormat:@"dd-MM-yyyy"]; //Here we can set the format which we need 
    NSString *convertedDateString = [dateFormatter stringFromDate:todayDate];// Here convert date in NSString
NSLog("Today formatted date is %@",convertedDateString);


回答by Beyaz

You can get like this

你可以这样

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:Date())
print(dateString)

回答by Feng Lin

try this ,set the different components unit:

试试这个,设置不同的组件单元:

    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents * components =
                        [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:today];
    NSInteger day = [components day];
    NSInteger month = [components month];
    NSInteger year = [components year];

if your want to get other days ,use this link calendar get other day

如果您想获得其他日子,请使用此链接 日历获得其他日子