Ios 比较字符串中日期的月份和日期与当前日期的月份和日期
我有一个JSON对象,它有几个日期,例如:15/12/2005或14/12/2012 我需要做一个月和日与今天的月和日的比较;年份无关紧要。我正在这样做:Ios 比较字符串中日期的月份和日期与当前日期的月份和日期,ios,objective-c,cocoa-touch,date,comparison,Ios,Objective C,Cocoa Touch,Date,Comparison,我有一个JSON对象,它有几个日期,例如:15/12/2005或14/12/2012 我需要做一个月和日与今天的月和日的比较;年份无关紧要。我正在这样做: -(void)setSingle:(NSDictionary*)object name:(NSString*)name { NSMutableDictionary * content = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standard
-(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];
}
}
-(无效)显示内容{
如果([self Days介于日期:currentDate和日期:self.dateLimit]>=0){
NSDictionary*Santodia=[超级比较:当前日期名称:@“santos”];
[自定义内容:[santododia objectForKey:@“texto”];
self.urlShare=[Santodia objectForKey:@“url”];
self.tituloShare=[santododia objectForKey:@“titulo”];
}否则{
[self.webView loadHTMLString:@“Conteúdo indeponível,escolha a data de hoje ou front”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];
}
-(void)defineContent:(NSString*)字符串{
string=[parentClass checkStringContent:string];
NSLog(@“Teste:%@”,字符串);
if(string.length==0){
字符串=@“无”;
}否则{
字符串=[NSString stringWithFormat:@“%@%@”,字符串,@“正文{color:#000!重要}。日{页边距顶端:5px}。月{页边距顶端:5px}”;
}
[self.webView loadHTMLString:string baseURL:nil];
}
试试这个来比较日期-
NSDate *minDate ;
NSDate *maxDate ;
NSComparisonResult compareStart = [minDate compare: self.selectedDate]; // date1 is 6/6/2011 00:00
NSComparisonResult compareEnd = [maxDate compare: self.selectedDate]; // date2 is 9/6/2011 00:00
if ( ( (compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame) ) && (compareEnd == NSOrderedDescending))
{
NSLog(@"date is in the right range");
}
else {
NSLog(@"Range is not valid .");
}
要仅比较日期的一部分,应使用。使用
NSDateFormatter
将字符串解析为NSDate
s:
NSString * jsonDateString = @"15/12/2005";
NSDateFormatter * dF = [NSDateFormatter new];
// Format string must match date string!
dF.dateFormat = @"dd/MM/yyy";
NSDate * testDate = [dF dateFromString:jsonDateString];
NSDate * today = [NSDate date];
从这里开始,您可以使用to和今天的日期来制作您感兴趣的作品:
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents * todayComps = [cal components:NSCalendarUnitMonth|NSCalendarUnitDay
fromDate:today];
NSDateComponents * testComps = [cal components:NSCalendarUnitMonth|NSCalendarUnitDay
fromDate:testDate];
然后可以比较这些日期组件对象:
[testComps isEqual:todayComps]
如果结果为
YES
,则两个日期具有相同的日期和月份,忽略日期的所有其他部分,包括年份。将日期格式化程序设置为[dateFormatter setDateFormat:@“dd/MM/yyyy”];那就行了,但我不想用这一年。请允许我详细说明。我有一个2015年12月15日的文本和另一个2002年12月16日的文本,如果您注意到年份不同,但重要的是什么以及日期和月份,将永远显示在所有日期16/12文本上,而不是年份