在 xcode 标签中创建日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5886807/
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
Create a date in xcode label
提问by shy
First of all I want to say thanks in advance for helping me.
首先,我想提前感谢您的帮助。
I want to know how can I create a date on a label using Xcode, and the date will follow the same date like in iphone.
我想知道如何使用 Xcode 在标签上创建日期,并且该日期将与 iphone 中的日期相同。
Thanks
谢谢
回答by johnoodles
Here's one simple approach
这是一种简单的方法
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//uncomment to get the time only
//[formatter setDateFormat:@"hh:mm a"];
//[formatter setDateFormat:@"MMM dd, YYYY"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
//get the date today
NSString *dateToday = [formatter stringFromDate:[NSDate date]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)];
[label setText:dateToday];
[formatter release];
//then add to a view
回答by Dan F
Creating the text date is simple, but the real question is what is your data source that you want to make the date text out of?
创建文本日期很简单,但真正的问题是,您希望使用什么数据源制作日期文本?
回答by Arvind Naladiga Venkat
The date can be formatted and set in label as below.
日期可以在标签中进行格式化和设置,如下所示。
NSDate *today = [NSDate date];
NSDateFormatter *dformat = [[NSDateFormatter alloc] init];
[dformat setDateFormat:@"dd:MM:YYYY"];
myLabel.text = [dformat stringFromDate:today];
回答by Krunal
NSDateFormatterhandle format of string dates.
NSDateFormatter处理字符串日期的格式。
Instances of NSDateFormatter create string representations of NSDate objects, and convert textual representations of dates and times into NSDate objects.
NSDateFormatter 的实例创建 NSDate 对象的字符串表示,并将日期和时间的文本表示转换为 NSDate 对象。
Try this and see:
试试这个,看看:
// Set date format according to your string date format
// e.g.: For,
// 22-12-1996 -> @"dd-MM-yyyy"
// 22/12/1996 -> @"dd/MM/yyyy"
// 1996-12-22 03:45:20 -> @"yyyy-MM-dd HH:mm:ss"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd/MM/yyyy";
//dateFormatter.dateFormat = @"dd-MM-yyyy";
NSDate *date = [dateFormatter dateFromString:dateString];
if(date == nil) {
correctFormat = false;
}
NSLog("Date: %@",date);
Note:Each pairs of characters in date format relates relevant date component with date instance. You can create any type of date format using date string pattern.
注意:日期格式中的每对字符将相关日期组件与日期实例相关联。您可以使用日期字符串模式创建任何类型的日期格式。
Here is document by Apple: Date Formatters
这是 Apple 的文档:Date Formatters
- Date (Day):dd
- Month:MM or MMM or MMMM
- Year: yy or yyyy
- 日期(Day):dd
- 月份:MM 或 MMM 或 MMMM
- 年: yy 或 yyyy
Here is list of date formats: Date Formats
这是日期格式列表:日期格式
Here is solution in Swift
这是Swift 中的解决方案
var today = Date()
var d_format = DateFormatter()
d_format.dateFormat = "dd:MM:yyyy"
label.text = dformat.string(from: today)