【发布时间】:2015-08-20 11:18:32
【问题描述】:
我有一个包含多个日期的 JSON 对象,例如:15/12/2005 或 14/12/2012。
我需要将月份和日期与今天的月份和日期进行比较;年份无所谓。我正在这样做:
-(void)setSingle:(NSDictionary*)object name:(NSString*)name {
NSMutableDictionary * content = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:name]];
NSLog(@"%@", name);
if(name == @"teste") {
NSDate *dateReturn = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM"];
NSString * tempDate;
NSDictionary *dateObjectTemp = nil;
NSDictionary * dateObject = [object objectForKey:name];
for (NSDictionary*g in dateObject) {
tempDate = [g objectForKey:@"dia"];
dateObjectTemp = g;
NSLog(@"Data: %@", tempDate);
dateReturn = [dateFormatter dateFromString:tempDate];
NSLog(@"Data: %@", dateReturn);
[content setObject:dateObjectTemp forKey:tempDate];
}
} else {
NSDate *dateReturn = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString * tempDate;
NSDictionary *dateObjectTemp = nil;
NSDictionary * dateObject = [object objectForKey:name];
for (NSDictionary*g in dateObject) {
tempDate = [g objectForKey:@"dia"];
dateObjectTemp = g;
dateReturn = [dateFormatter dateFromString:tempDate];
[content setObject:dateObjectTemp forKey:tempDate];
}
}
NSDictionary* cont =content;
[[NSUserDefaults standardUserDefaults] removeObjectForKey:name];
[[NSUserDefaults standardUserDefaults] setObject:cont forKey:name];
}
这会获取日期,但 dataReturn 返回 2000-12-15。我只想要日期和月份。
什么时候比较下一个方法
-(void)showContent{
if ( [self daysBetweenDate:currentDate andDate:self.dateLimit]>=0 ) {
NSDictionary *santododia = [super compareDate:currentDate name:@"santos"];
[self defineContent:[santododia objectForKey:@"texto"]];
self.urlShare = [santododia objectForKey:@"url"];
self.tituloShare = [santododia objectForKey:@"titulo"];
}else{
[self.webView loadHTMLString: @"<font color='black' style='font-family:sans-serif;'>Conteúdo indisponível, escolha a data de hoje ou anterior</font>" baseURL:nil];
}
}
虽然当前日期匹配,但测试未通过。
-(void) defineContent:(NSString *)string{
string = [parentClass checkStringContent:string];
NSLog(@"Teste: %@", string);
if(string.length == 0){
string = @"<font color='black' style='font-family:sans-serif;'>nothing</font>";
} else {
string = [NSString stringWithFormat: @"%@ %@", string , @"<style>body {color:#000 !important} .day{margin-top:5px} .month{margin-top:5px} </style>" ];
}
[self.webView loadHTMLString: string baseURL:nil];
}
【问题讨论】:
-
设置日期格式为 [dateFormatter setDateFormat:@"dd/MM/yyyy"];然后就可以正常工作了。
-
但我不想使用年份。请允许我详细说明。我有一个文本 2015 年 12 月 15 日和 2002 年 16 月 12 日的另一天,如果您注意到不同的年份,但重要的是日期和月份,永远将显示在 16 年 12 月的所有日子文本,与年份无关
标签: ios objective-c cocoa-touch date comparison