我想知道工作日这个2017-01-01T23:33:09+0800
格式的时间,我在一个字符串中,所以我可以比较当前日期并显示它像昨天,今天,星期一等。
答案 0 :(得分:0)
NSString *myDateString = @"2017-01-11T23:33:09+0800";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; // you have to set your dateformatter according your requirement.
NSDate *date1 = [dateFormatter dateFromString:myDateString];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date1];
NSInteger weekday = [components weekday];
NSString *weekdayName = [dateFormatter weekdaySymbols][weekday - 1];
// here you get your date day like sunday , monday , etc.
NSLog(@"%@ is a %@", myDateString, weekdayName);
// check for your date is today , yesterday or Tommorow.
NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
if ([calender isDateInToday:date1]) {
NSLog(@"Today");
}
else if ([calender isDateInYesterday:date1])
{
NSLog(@"Yesterday");
}else if([calender isDateInTomorrow:date1]){
NSLog(@"Tommorow");
}
输出
2017-01-11T23:33:09+0800 is a Wednesday
Yesterday
答案 1 :(得分:0)
这是用于将DateString转换为weekDay
的快捷3.0代码:
let myDateString = "2017-01-01T23:33:09+0800"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZ"
let date = dateFormatter.date(from: myDateString)!
let unitFlags = Set<Calendar.Component>([.weekday])
var components = Calendar.current.dateComponents(unitFlags, from: date)
let weekday = components.weekday!
let weekdayName = dateFormatter.weekdaySymbols[weekday - 1]
print("\(myDateString) is a \(weekdayName)")
<强>输出:强> 2017-01-01T23:33:09 + 0800是星期天
希望这会对你有所帮助。