How to get day and date?
i want to ge开发者_开发百科t date in 2 labels like this
Label1:-> Tuesday Label2:-> 2 June 2010how can i get this value?
This code will get you the strings you require to set the labels:
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];
The date formatters are controlled through this standard: Unicode Date Formats
[label1 setText:@"Tuesday"];
[label2 setText:@"2 June 2010"];
Or did you need something different?
Hello You can get Day and Date From this code and here weekday give you the day Example: Monday,Tuesday for re
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];
In swift, you can do like that:
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)
精彩评论