Working with NSDate and Comparing with Dates in Objective C - iphone

"31-Dec-2010 9:00AM to 1:00PM"
Take the above NSString for example, I need to convert it to 2 NSDates e.g.
31-Dec-2010 9:00AM AND 31-Dec-2010 1:00PM
Then compare it with the current Date to see if the current date falls within the given dates.
So 31-Dec-2010 10:00AM would fall within.
I'm wondering What are the best practices/tricks are with Objective C to do this elegantly?

As it turns out, NSDateFormatter has a dateFromString method, which does exactly what you want.
http://blog.evandavey.com/2008/12/how-to-convert-a-string-to-nsdate.html
The official documentation for NSDateFormatter:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

I ended up doing it like so:
NSString *temp = [[dateString allValues] objectAtIndex:0];
NSLog(#"temp: %#", temp);
NSArray *tokens = [temp componentsSeparatedByString: #" "];
NSArray *tokenOneDelimited = [[tokens objectAtIndex:0] componentsSeparatedByString: #"-"];
NSString *dateStr1 = [NSString stringWithFormat: #"%#-%#-%# %#", [tokenOneDelimited objectAtIndex:2],
[tokenOneDelimited objectAtIndex:1],
[tokenOneDelimited objectAtIndex:0],
[tokens objectAtIndex:1]];
NSString *dateStr2 = [NSString stringWithFormat: #"%#-%#-%# %#", [tokenOneDelimited objectAtIndex:2],
[tokenOneDelimited objectAtIndex:1],
[tokenOneDelimited objectAtIndex:0],
[tokens objectAtIndex:3]];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MMM-dd hh:mma"];
NSDate *myDate1 = [dateFormatter dateFromString: dateStr1];
NSDate *myDate2 = [dateFormatter dateFromString: dateStr2];
NSDate *currentDate = [NSDate date];
NSComparisonResult comparison = [currentDate compare: myDate1];
NSComparisonResult comparison2 = [currentDate compare: myDate2];
if (
comparison == NSOrderedDescending &&
comparison2 == NSOrderedAscending
)
{
NSLog(#"is On now");

Related

Comparing two dates for daily reward in iOS game

I was implementing a 'daily reward' method in order to give some coins every day in my game.
I have a json that contains the actual date and time.
The issue is that I don't know how to compare the dates.
Here is my code -
#implementation ItemRewardManager
+(NSDate *)dateFromUTCTimeStamp:(NSString *)dateString{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *myDate = [df dateFromString: dateString];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[df setTimeZone:gmt];
[[NSUserDefaults standardUserDefaults] setObject:myDate forKey:#"lastDatePlayed"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)getData
{
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:dateUrl]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
NSArray *keys = [jsonObjects allKeys];
// values in foreach loop
for (NSString *key in keys) {
NSLog(#"%# is %#",key, [jsonObjects objectForKey:key]);
}
}
+(void)dailyReward{
NSLog(#"llega al daily");
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"HH:mm:ss zzz"];
NSDate *now = [NSDate date];
NSString *dateString = [dateFormat stringFromDate:now];
if([[[NSUserDefaults standardUserDefaults] objectForKey:#"lastDatePlayed"] isEqualToString: dateString])
{
NSLog(#"Actual date is the same as json date");
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:nil
message:#"You have win 5 coins! Come back tomorrow for more."
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil] autorelease];
[alert show];
return;
}
}
#end
So, when I open my app and call this method, my app crashes for some reasone.
I think the problem might be on the comparison of the two dates.
Can someone tell me if there is something wrong?
Use following.
if ([date1 compare:date2]==NSOrderedSame)
date1 and date2 are NSdate objects.
NSDate has methods for comparing dates.
Here they are:
isEqualToDate: Returns a Boolean value that indicates whether a given
object is an NSDate object and exactly equal the receiver.
- (BOOL)isEqualToDate:(NSDate *)anotherDate
laterDate: Returns the later of the receiver and another given date.
- (NSDate *)laterDate:(NSDate *)anotherDate
earlierDate: Returns the earlier of the receiver and another given
date.
- (NSDate *)earlierDate:(NSDate *)anotherDate
Try like this. It may help you
NSDate *now = [NSDate date]; // current date
NSDate *newDate = [dateFormat dateFromString:[[NSUserDefaults standardUserDefaults] objectForKey:#"lastDatePlayed"]]; // server date
NSComparisonResult result;
result = [now compare:newDate]; // comparing two dates
if(result == NSOrderedAscending)
NSLog(#"now is less");
else if(result == NSOrderedDescending)
NSLog(#"newDate is less");
else
NSLog(#"Both dates are same");
Use this code.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy"];
NSDate *ServerDate = [dateFormatter1 dateFromString:#"03/01/2013"];
[dateFormatter1 release];
NSTimeInterval interval = [date timeIntervalSinceDate:ServerDate];
int diff=interval/1440;
This is implemented code.

issue in Changing date formate

i have one array of string something like below
newlymadeArray(
"03/05/2013",
"09/22/2013",
"03/02/2013",
"03/02/2013",
"08/22/2013",
"11/02/2013",
"07/08/2013",
"08/31/2013",
"05/13/2013",
"07/11/2013",
"10/07/2013",
"02/20/2013"
)
current formate is MM/dd/yyyy
i want to change it to dd/MM/yyyy
i am using following code for this
NSLog(#"_newlymadeArray%#",_newlymadeArray);
//logic for changing date formate
for( int i=0; i<_newlymadeArray.count; i++){
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy"];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MM/yyyy"];
NSDate *old = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSLog(#"old %#",old);
NSString *old1 =[dateFormatter1 stringFromDate:old];
NSDate *new = [dateFormatter2 dateFromString:old1];
NSLog(#"new %#",new);
NSString *string = [dateFormatter2 stringFromDate:new];
NSLog(#"string %#",string);
[_convertedBdates addObject:string];
}
NSLog(#"_convertedBdates%#",_convertedBdates);
this is what im getting as output
old 2013-03-04 18:30:00 +0000
new 2013-05-02 18:30:00 +0000
string 03/05/2013
old 2013-09-21 18:30:00 +0000
new (null)
string (null)
] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
my questions are why old get printed 2013-03-04 instead of 05/03/2013
at the end of first time loop it gets printed 03/05/2013 instead of 05/03/2013
and when loop is running second time it gets null value in new so thats why adding null value in array program get crashed
plz tell me what im doing wrong ?
This is never going to be appropriate:
NSString *old1 =[dateFormatter1 stringFromDate:old];
NSDate *new = [dateFormatter2 dateFromString:old1];
You're saying, "Format a date using one formatter... and then parse it using a different one."
You've already parsed the value which was in the old format. You now need to format it in the new formatter.
I'm not an Objective-C developer, but I suspect you just want:
NSDate *date = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSString *string = [dateFormatter2 stringFromDate:date];
[_convertedBdates addObject:string];
Note how there's only one date involved, but two strings (the original format and the new format).
Also note that there's no need to create a new pair of formatters on each iteration of the loop. Create them before the loop and reuse them:
NSDateFormatter *oldFormatter = [[NSDateFormatter alloc] init];
[oldFormatter setDateFormat:#"MM/dd/yyyy"];
NSDateFormatter *newFormatter = [[NSDateFormatter alloc] init];
[newFormatter setDateFormat:#"dd/MM/yyyy"];
for (int i = 0; i < _newlymadeArray.count; i++) {
NSDate *date = [oldFormatter dateFromString:[_newlymadeArray objectAtIndex:i]];
NSString *string = [newFormatter stringFromDate:date];
[_convertedBdates addObject:string];
}
(You may also want to check whether date is null within the loop, to handle bad data, of course.)
Try this one:
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy"];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MM/yyyy"];
for( int i=0; i<_newlymadeArray.count; i++){
NSDate *old = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSLog(#"old %#",old);
if (old) {
NSString *newDateString = [dateFormatter2 stringFromDate:old];
NSLog(#"new %#",newDateString);
[_convertedBdates addObject:newDateString];
}
}
[dateFormatter1 release];
[dateFormatter2 release];
NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc] init];
[fromDateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDateFormatter *toDateFormatter = [[NSDateFormatter alloc] init];
[toDateFormatter setDateFormat:#"dd/MM/yyyy"];
for (NSString *dateString in newlyMadeArray)
{
NSDate *date = [fromDateFormatter dateFromString:dateString];
NSString *newDateString = [toDateFormatter stringFromDate:date];
[_convertedBdates addObject:newDateString];
}
First you should convert the string "MM/dd/yyyy" to "dd/MM/yyyy".
for( int i=0; i<_newlymadeArray.count; i++){
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy"];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MM/yyyy"];
NSDate *old = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSLog(#"old %#",old);
NSArray *arr = [[_newlymadeArray objectAtIndex:i] componentsSeparatedByString:#"/"];
NSString *old1 = [NSString stringWithFormat:#"%#/%#/%#",[arr objectAtIndex:1],[arr objectAtIndex:0],[arr objectAtIndex:2]];
NSDate *new = [dateFormatter2 dateFromString:old1];
NSLog(#"new %#",new);
NSString *string = [dateFormatter2 stringFromDate:new];
NSLog(#"string %#",string);
[_convertedBdates addObject:string];
}

Converting a value into NSDate

I have this ticks value "634758517020305000" which corresponds to 21st June 2012. I tried to convert the tick value into NSDate object like this:
NSString *str = #"634758517020305000";
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSDate *date = [[NSDate dateWithTimeIntervalSince1970:
[[str substringWithRange:NSMakeRange(0, [str length])] intValue]]
dateByAddingTimeInterval:offset];
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSString *fourDigitYearFormat = [[dateFormatter dateFormat] stringByReplacingOccurrencesOfString:#"yy" withString:#"yyyy"];
[dateFormatter setDateFormat:fourDigitYearFormat];
}
// There you have it:
NSString *outputString = [dateFormatter stringFromDate:date];
But I got the output: 1/19/2038. If anyone knows where I am doing wrong, please let me know.
your Unix timestamps is wrong. it should be 1340236800
check your Unix timestamps Here

Sort NSString of NSDate in iphone

I have an array which has NSString converted from NSDate. I am getting the initial index of the array and sorting them.
Dates are in format : 08AM , 01PM. When I try to do NSSortDescriptor, it always gives me 01PM and than 08AM.
//** Date string **//
NSDate *date1 = [dateFormat dateFromString:startSection];
[dateFormat setDateFormat:#"ah"];
startSection = [dateFormat stringFromDate:date1];
NSString *strSection = [NSString stringWithFormat:#"%#", startSection];
The string above is added in NSArray. The NSArray is used below to sort the strings. And finally, the same array is used to get the initial letter and display their index in sectionforsectionIndexTitle. So I have AM8 and PM1 as sections and not 8AM and 1PM.
NSSortDescriptor *nameDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"start"
ascending:FALSE
selector:#selector(localizedCaseInsensitiveCompare:)] autorelease] ;
NSArray *descriptors = [NSArray arrayWithObject:nameDescriptor];
[[dict objectForKey:aKey] sortUsingDescriptors:descriptors];
How about using sortUsingComparator:? But this will involve changing the string from 08AM to AM08 and 01PM to PM01 before comparison so this assumes that they will be of that form.
NSMutableArray * theArray = [dict objectForKey:aKey]; /* To be sorted */
[theArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString * string1 = [NSString stringWithFormat:#"%#%#", [(NSString *)obj1 substringFromIndex:2], [(NSString *)obj1 substringToIndex:2];
NSString * string2 = [NSString stringWithFormat:#"%#%#", [(NSString *)obj2 substringFromIndex:2], [(NSString *)obj2 substringToIndex:2];
return [string1 localizedCaseInsensitiveCompare:string2];
}];
/* theArray is now sorted */

ALAsset timestamp returns wrong date

I am trying to get the timestamp of images, I can get the correct latitude and longitude values, but the timestamp always returns the current time, not the EXIF time of the image.
ALAssetsLibraryAssetForURLResultBlock resultsBlock = ^(ALAsset *asset) {
CLLocation *imageLoc = [asset valueForProperty:ALAssetPropertyLocation];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/YY HH:mm:ss"];
NSString *trailTime = [formatter stringFromDate:imageLoc.timestamp];
NSLog(#"---+++ image TimeStamp: %#", trailTime);
[formatter release];
Any help appreciated, thanks
You will need to get the date using ALAssetPropertyDate key.
NSDate * date = [asset valueForProperty:ALAssetPropertyDate];
/* Use the `NSDateFormatter` instance to print the date */
OK I found the answer
What gave me the entire metadata in dictionary format was:
NSDictionary *metadata = asset.defaultRepresentation.metadata;
//Hope this helps others.
It seems like you're fetching location to get the date. you should be doing something like the following:
ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
[assetsLib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
//If you'd want to directly fetch from it's external property, which seems more appropriate.
NSDate *date = [result valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"dd-mm-yyyy hh:mm:ss ZZZ"];
NSString *stringDate = [dateFormatter stringFromDate:date];
//If you'd want to fetch the date from metaData
ALAssetRepresentation *assetRep = [result defaultRepresentation];
NSDictionary *dictionaryOfMetaData = [assetRep metadata];
NSLog(#"dictionary:%# \n \
date:%# \n \
StringDate:%#", [[dictionaryOfMetaData valueForKey:#"{TIFF}"] valueForKey:#"DateTime"],
date,
stringDate);
}];
}
failureBlock:^(NSError *error) {
//Handle Error!
}];