从日期编号中获取一周中的一天的名称。 (IOS)

时间:2016-08-03 07:50:46

标签: ios objective-c nsdate nsdateformatter

我想找出一种方法来获取当天的名字(例如:星期一,星期二......),从星期一开始(例如:星期一为1,星期二为2 ......)。

我有NSDictionary这样的结构。

{
close =     {
    day = 5;
    time = "22:00";
    dayName = "";
};
open =     {
    day = 5;
    time = "9:00";
    dayName = "";
};
}

我想在密钥" dayName" ...下添加当天的名字,其中值是与密钥中的日期编号对应的日期的名称"天"

谢谢:)

4 个答案:

答案 0 :(得分:6)

让我们从获取日期的名称开始:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSArray *daySymbols = dateFormatter.standaloneWeekdaySymbols;

现在,这是一个工作日名称数组,其中索引0是星期日,因此您必须转换索引。

NSInteger dayIndex = 1; // 1 = Monday, ... 7 = Sunday
NSString *dayName = daySymbols[dayIndex % 7];

答案 1 :(得分:0)

应用枚举并在数组中添加日期

NSString * closeDay=[[yourDictionary valueForKey:@"close"] valueForKey:@"dayName"];
NSString * openDay=[[yourDictionary valueForKey:@"open"] valueForKey:@"dayName"];

NSMutableArray *arr = [[NSMutableArray alloc]init];

[arr addObject:openDay];
NSMutableArray *finalArray= [[NSMutableArray alloc]init];

for(int i=0; i<[givenDic count];i++){
NSString *day= [self getDay:arr[i]];
NSMutableDictionary *dic= [[NSMutableDictionary alloc]init];
[dic setObject:day forKey:@"dayName"];
[finalArray addObject:dic];
}

-(NSString*)getDay:(NSString *)dayNo{
switch([dayNo intValue]){

case 1:
return Monday;
break;

case 2:
return Tuesday;
break;

case 3:
return Wednesday;
break;

case 4:
return Thursday;
break;

case 5:
return Friday;
break;

case 6:
return Saturday;
break;

case 7:
return Sunday;
break;
}
}

答案 2 :(得分:-1)

NSString * closeDay=[[yourDictionary valueForKey:@"close"] valueForKey:@"dayName"];
NSString * openDay=[[yourDictionary valueForKey:@"open"] valueForKey:@"dayName"];

如果要检查,可以将dayName替换为day。我希望我能帮忙;

答案 3 :(得分:-1)

NSMutableArray *array = [NSMutableArray new];
[array addObject:@"1"];
[array addObject:@"2"];
[array addObject:@"5"];
[array addObject:@"4"];

NSMutableArray *mainArray = [NSMutableArray new];

for(int i=0; i< array.count;i++){
    NSMutableDictionary *dict = [NSMutableDictionary new];
    switch ([array[i] integerValue]) {
        case DayOne:
            [dict setObject:array[i] forKey:@"day"];
            [dict setObject:@"your time" forKey:@"time"];
            [dict setObject:@"Sunday" forKey:@"dayName"];
            break;
        case DayTwo:
            [dict setObject:array[i] forKey:@"day"];
            [dict setObject:@"your time" forKey:@"time"];
            [dict setObject:@"Monday" forKey:@"dayName"];
            break;
        case DayThree:
            [dict setObject:array[i] forKey:@"day"];
            [dict setObject:@"your time" forKey:@"time"];
            [dict setObject:@"Tuesday" forKey:@"dayName"];
            break;
        case DayFour:
            [dict setObject:array[i] forKey:@"day"];
            [dict setObject:@"your time" forKey:@"time"];
            [dict setObject:@"Wednesday" forKey:@"dayName"];
            break;
        case DayFive:
            [dict setObject:array[i] forKey:@"day"];
            [dict setObject:@"your time" forKey:@"time"];
            [dict setObject:@"Thursday" forKey:@"dayName"];
            break;
        case DaySix:
            [dict setObject:array[i] forKey:@"day"];
            [dict setObject:@"your time" forKey:@"time"];
            [dict setObject:@"Friday" forKey:@"dayName"];
            break;
        case DaySeven:
            [dict setObject:array[i] forKey:@"day"];
            [dict setObject:@"your time" forKey:@"time"];
            [dict setObject:@"Saturday" forKey:@"dayName"];
            break;
        default:
            break;
    }
    [mainArray addObject:dict];
}
NSLog(@"%@",mainArray);

typedef NS_ENUM(NSInteger,DayString){     DayOne = 1,     第二天,     DayThree,     DayFour,     DayFive,     DaySix,     DaySeven };