Calendar日历 NSDateFormatter日期相关方法

Calendar日历 NSDateFormatter日期相关方法



- (NSString *)getWeekStringWithDateString:(NSString *)dateString
{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [format dateFromString:dateString];
[format setDateFormat:@"yyyy-MM-dd-EEEE"];
NSString *sss = [[format stringFromDate:date] componentsSeparatedByString:@"-"][3];
NSInteger index = 0;
if ([sss rangeOfString:@"星期"].location != NSNotFound)
index = [@[@"星期一", @"星期二", @"星期三", @"星期四" , @"星期五", @"星期六", @"星期日"] indexOfObject:sss];
else
index = [@[@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday"] indexOfObject:sss];
return @[@"周一", @"周二", @"周三", @"周四", @"周五", @"周六", @"周日"][index];
}





NSDate *censorTimeDate = [NSDate dateWithTimeIntervalSince1970:[aMode.add_time doubleValue]/1000];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd hh:mm"];
self.timeLabel.text = [dateFormatter stringFromDate:censorTimeDate];





// 日期格式化

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEEddMMMM" options:0 locale:nil];
self.selectedDateLabel.text = [formatter stringFromDate:self.datepicker.selectedDate];


//日期是否周末
- (BOOL)isWeekday:(NSDate *)date
{
NSInteger day = [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:date] weekday];

const NSInteger kSunday = 1;
const NSInteger kSaturday = 7;

BOOL isWeekdayResult = day == kSunday || day == kSaturday;

return isWeekdayResult;
}

//日期是否今天
- (BOOL)isToday:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:date];
NSDate *otherDate = [cal dateFromComponents:components];

if([today isEqualToDate:otherDate]) {
return YES;
} else {
return NO;
}
}


// 年月日

- (void)setDate:(NSDate *)date
{
_date = date;

[self.dateFormatter setDateFormat:@"MM.dd"];
NSString *monthFormattedString = [self.dateFormatter stringFromDate:date];

[self.dateFormatter setDateFormat:@"d"];

[self.dateFormatter setDateFormat:@"EEE"];
NSString *dayInWeekFormattedString = [self.dateFormatter stringFromDate:date];

self.dayLabel.text = monthFormattedString;


self.topLabel.text = [self getWeekDay:dayInWeekFormattedString];

}


//星期几

- (NSString *)getWeekDay:(NSString *)weekday
{
NSArray *arrWeek=[NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];

NSArray *aWeek = [NSArray arrayWithObjects:@"Sun",@"Mon",@"Tue",@"Wed",@"Thu",@"Fri",@"Sat", nil];

NSInteger index = [aWeek indexOfObject:weekday];

return [arrWeek objectAtIndex:index];
}






Loading Disqus comments...
Table of Contents