开发者

problem for finding same date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

i am geeting date from db and convert into date format from string i.e currentdatestring and converted date is dbdate and then i am taking current date and convert in to string and then again into date format i.e curdate when i am printing this both variable to log then it give me differnet time why actuall just i want to compare that my dbdate is equal to current date is not?

NSDate  *currentdate=[NSDate date];
NSDateFormatter *dtformater=[[NSDateFormatter alloc] init];
[dtformater setDateFormat:@"yyyy-MM-dd 00:00:00"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
[dtformater setTimeZone:timeZone];

NSString *currentdatestring=[dbObj selectdatetable:1];
NSLog(@"currentdatestring== %@",currentdatestring);
NSDate *dbdate= [[NSDate alloc] init];
dbdate = [dtformater dateFromString:currentdatestring];

NSDate *dd=[NSDate date];
NSString *str=[dtformater stringFromDate:dd];
NSDate *curdate=[dtformater dateFromString:str];
NSLog(@"dbdate==%@",dbdate);
NSLog(@"curdate == %@",curdate);
MYLOG is
2011-09-17 14:27:59.261 PW[9335:207] currentdatestring== 2011-09-17
2011-09-17 14:27:59.262 PW[9335:207] dbdate==2011-09-17 00:00:00 +0000
2011-09-17 14:27:59.263 PW[9335:207] currentdate == 2011-09-17 08:57:59 +0000
2011-09-17 14:27:59.263 PW[9335:207] in first row  day 开发者_如何学Pythonis NOT NOT NOT same

what's problem i can't understand ,please help me..


You are trying to compare a date which has only the date to another date which has a date and time. Ignoring the memory leaks that you have in your code snippets try something like this:

// Configure the formatter
NSDate  *currentdate=[NSDate date];
NSDateFormatter *dtformater=[[NSDateFormatter alloc] init];
[dtformater setDateFormat:@"yyyy-MM-dd"];

// Get date from the database
NSString *currentdatestring=[dbObj selectdatetable:1];
NSLog(@"currentdatestring== %@",currentdatestring);
NSDate *dbdate = [dtformater dateFromString:currentdatestring];

// Get the current date and time
NSDate *dd=[NSDate date];

// Transform this to just a date
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:dd];
NSDate* dd = [calendar dateFromComponents:components];

NSString *str=[dtformater stringFromDate:dd];
NSLog(@"dbdate==%@",dbdate);
NSLog(@"curdate == %@", dd);

I'll leave you to do the memory management to tidy this up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜