#pragma mark - 获取年月日及星期
+ (NSString *)stringFromDate
{
// 添加日期
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-dd"];
comps = [calendar components:unitFlags fromDate:senddate];
NSString *locationString=[dateformatter stringFromDate:senddate];
NSString *date_weekStr = [NSString stringWithFormat:@"%@ %@",locationString,[self week:[comps weekday]]];
return date_weekStr;
}
#pragma mark - 推算星期
+(NSString *)week:(NSInteger)week
{
NSString *weekStr=nil;
if(week==1)
{
weekStr=@"星期天";
}else if(week==2){
weekStr=@"星期一";
}else if(week==3){
weekStr=@"星期二";
}else if(week==4){
weekStr=@"星期三";
}else if(week==5){
weekStr=@"星期四";
}else if(week==6){
weekStr=@"星期五";
}else if(week==7){
weekStr=@"星期六";
}
return weekStr;
}
IOS 获取年月日、星期
最新推荐文章于 2023-09-02 08:47:01 发布