objective-c 我如何获得当前日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2101648/
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-09-03 22:48:56  来源:igfitidea点击:

How do I get the current date?

iphoneobjective-ccocoacocoa-touchdate

提问by Mat

How can I get the current date, and format it like "20/12/2010"?

如何获取当前日期,并将其格式化为“20/12/2010”?

回答by martin clayton

Use NSDateand NSDateFormatter.

使用NSDateNSDateFormatter

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"date: %@", dateString);

See also.

也可以看看。

回答by Mike Abdullah

Break it down.

打破它。

How do I get the current date?See NSDate.

我如何获得当前日期?请参阅NSDate

How do I format a date in a specific format?See NSDateFormatter.

如何以特定格式格式化日期?请参阅NSDateFormatter

回答by J-Dizzle

Here is a method to retrieve the current date and print as a string using Swift on IOS 9

这是一种在 IOS 9 上使用 Swift 检索当前日期并打印为字符串的方法

    let formatter : NSDateFormatter = NSDateFormatter()
    formatter.dateFormat = "d/M/yy"
    let myStr : String = formatter.string(from:   NSDate.init(timeIntervalSinceNow: 0) as Date)
    print(myStr)

XCode Debug Print

XCode 调试打印

5/6/16