NSDate as argument in customMethod - iphone

I've defined my custom method ( -(void)loadXML {} ) into my appDelegate.
Now I'd like to use it in severals viewControllers; Right now I'm using local NSDate objects.
NSDate *todayDate = [NSDate date];
NSString *XMLUrl = #"http://localhost/MyApp/GetXML?&aDate=";
NSString *urlString = [NSString stringWithFormat:#"%#%#", XMLUrl, todayDate];
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:urlString]];
instead of 'todayDate' I'd like to have 'selectedDate'; also how I add a bool to my method, need to have some conditions into my method?

Here's what you can do:
NSDate *selectedDate = ???????; // set this to whatever you want selectedDate to be
BOOL myBoolean = YES;
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: #"yyyy-MM-dd"]; // or whatever format you wish
NSString *urlString =
[NSString stringWithFormat:#"http://localhost/MyApp/GetXML?BOOL=%#&aDate=%#",
(myBoolean ? #"YES" : #"NO"),
[dateFormatter stringFromDate: selectedDate]];
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:urlString]];
[dateFormatter release]; // don't forget to release, if not using ARC
And for your benefit, I'm showing you how to use a NSDateFormatter and a generic C ternary conditional.
I hope this helps you out!

Related

How to convert the NSString of format (2013-07-25T06:15:33.180-04:00) to NSDate?

How to convert the NSString of format (2013-07-25T06:15:33.180-04:00) to NSDate?
I am trying to convert using the following code
NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init];
[rfc3339TimestampFormatterWithTimeZone setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[rfc3339TimestampFormatterWithTimeZone setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss ZZZ"];
NSDate *theDate = nil;
NSError *error = nil;
if (![rfc3339TimestampFormatterWithTimeZone getObjectValue:&theDate forString:dateString range:nil error:&error]) {
NSLog(#"Date '%#' could not be parsed: %#", dateString, error);
}
NSString *strDate = [rfc3339TimestampFormatterWithTimeZone stringFromDate:theDate];
return strDate;
Please help me
try like this,i hope you'l succeed..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-07-25T06:15:33.180-04:00"];//chnage symbal here.
NSLog(#"%#",date);

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];
}

how to concatenate NSString and NSDateFormatter

I need to concatenate a NSString with the NSString output of NSDateFormatter. My code is below.
Please check where I am going wrong. I have to concatenate DEST PATH with datestring.
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
// below is for concatenate
result =[result stringByAppendingString:DEST_PATH];
result=[result stringByAppendingString:dateString];
It appears as though you never instantiate the NSString result. I would change the above to the following:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
NSString* result=[DEST_PATH stringByAppendingPathComponent:dateString];
If you use the stringByAppendingPathComponent method it will automatically add the slash for you.
Also, using all capital letters for identifiers usually denotes a c style propocessor macro constant, so using it as a variable may confuse some people.
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
// below is for concatenate
NSString *resultStr=[NSString stringWithFormat:#"%#%#",DEST_PATH,dateString];
Hope this helps you.

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!
}];

Working with NSDate and Comparing with Dates in Objective C

"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");