如何获得日期和日期?

时间:2010-06-09 17:21:45

标签: objective-c iphone

我想在这样的2个标签中获取日期 label1的: - >周二
LABEL2: - > 2010年6月2日

我怎样才能得到这个值?

4 个答案:

答案 0 :(得分:6)

此代码将为您提供设置标签所需的字符串:

NSDate *today = [NSDate date];
NSDateFormatter *weekdayFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: @"d MMMM yyyy"];
[weekdayFormatter setDateFormat: @"EEEE"];
NSString *formattedDate = [formatter stringFromDate: today];
NSString *weekday = [weekdayFormatter stringFromDate: today];

日期格式化程序通过此标准进行控制:Unicode Date Formats

答案 1 :(得分:4)

[label1 setText:@"Tuesday"];
[label2 setText:@"2 June 2010"];

或者您需要不同的东西吗?

答案 2 :(得分:2)

您好,您可以从此代码获取日期和日期 这里的工作日给你一天的例子:周一,周二再来

NSDate *today = [NSDate date];
NSDateFormatter *weekdayFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: @"d MMMM yyyy"];
[weekdayFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[weekdayFormatter setDateFormat: @"EEEE"];
NSString *formattedDate = [formatter stringFromDate: today];
NSString *weekday = [weekdayFormatter stringFromDate: today];

答案 3 :(得分:0)

很快,您可以这样做:

    let today = Date()
    let weekdayFormatter = DateFormatter()
    let formatter = DateFormatter()
    formatter.dateFormat = "d MMMM yyyy"
    weekdayFormatter.dateFormat = "EEEE"
    let formattedDate = formatter.string(from: today)
    let weekday = weekdayFormatter.string(from: today)