Here is my string stored in the value below: 2011-03-13 23:22:05
Here is my code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter dateFromString: [dict valueForKey:#"Message"]];
[m setLastSyncDate:[formatter dateFromString: [dict valueForKey:#"Message"]]];
NSLog(#"New LastSyncDate: %# : %#",[dict valueForKey:#"Message"], m.LastSyncDate);
This is the response I get in console:
New LastSyncDate: 2011-03-13 23:22:05 : (null)
Why am I getting a (null) here?
UPDATE CODE:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
Manufacturer *m = [self.manufacturers objectAtIndex:i];
[m setLastSyncDate:[formatter dateFromString: [dict valueForKey:#"Message"]]];
NSLog(#"New LastSyncDate: %# : %#",[formatter dateFromString: [dict valueForKey:#"Message"]], m.LastSyncDate);
RETURNS:
New LastSyncDate: (null) : (null)
updated code:
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//2011-03-13 23:22:05
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; ///add this line
NSString* message;
NSDate* LastSyncDate;
message=#"2011-03-13 23:22:05";
LastSyncDate=[formatter dateFromString:message];
NSLog(#"New LastSyncDate: %# : %#",message, LastSyncDate);
You need to set the date format string.
You also need to make sure that [dict valueForKey:#"Message"] isn't returning nil.
Related
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);
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];
}
I have to convert the given time in 24 hr format.But I did not get it.Don't know where I m going wrong.
If I have TimeStart as 1:15 PM then I should get 13:15 ..
Item *item=[sectionItems objectAtIndex:itemId];
NSLog(#"%#",item.TimeStart);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm a"];
NSString *formattedDateString = [dateFormatter stringFromDate:item.TimeStart];
NSLog(#"%#",formattedDateString);
In the code above I have a Item class and that I have NSString *TimeStart;
When I see formattedDateString it is returning null.How can I fix it ?
Item *item=[sectionItems objectAtIndex:itemId];
NSLog(#"%#",item.TimeStart);
NSString *str = #"1:45.PM";// put here item.TimeStart
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm.a"];// here give the format which you get in TimeStart
NSDate *date = [dateFormatter dateFromString: str];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH:mm"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
i just simply setDateFormat like [formatter setDateFormat:#"HH:mm"]; Remove a
and below caode for checking its time formate is 24 hr or 12
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
[formatter release];
NSLog(#"%#\n",(is24h ? #"YES" : #"NO"));
Use this to set 24 hr format.
[currentDate setDateFormat:#"yyyy-MM-dd HH:mm"];
or
[formatter setDateFormat:#"HH:mm"];
without using formatter
NSRange range;
range.location = 2;
range.length = 2;
NSString *dateString=[NSString stringWithFormat:#"%#:%#",[myString substringToIndex:2], [myString substringWithRange:range]];
This is the code i am using in my project and its working for me
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Current time date:%#", dateString);
output:Current time date:14:41
I am trying to convert from an NSString to NSDate using getObjectValue:forString:range:error:. The function returns success and no errors, but the returned date is incorrect.
Specifically, I am passing #"1989-12-31T00:00:00+00:00" to the function below and the returned date is 1970-01-01 00:00:000 +0000. Any thoughts?
+ (NSDate *) convertStringToDate:(NSString *) dateString
{
NSDate* date = nil;
NSError* error = nil;
NSRange range = NSMakeRange(0, [dateString length]);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
BOOL success = [dateFormatter getObjectValue:&date forString:dateString range:&range error:&error];
[dateFormatter release];
return success ? date : nil;
}
Thanks in advance.
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:locale];
[dateFormatter setLocale:usLocale];
[usLocale release];
[dateFormatter setDateFormat:#"EEE, dd MMMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
Notes:
Get locale for NSLocale init from source you're getting this date like RSS feed or similar.
setDateFormate: adjust to format of your source.
I trying to find a way to convert a string into a date with a given locale identifier.
I have for example an italian date (locale: it_IT) I want to convert into a valid date object.
NSDate *GLDateWithString(NSString *string, NSString *localeIdentifier) {
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
[formatter setLocale:locale];
[locale release];
NSDate *date = [formatter dateFromString:string];
[formatter release];
return date;
}
this code does not work, the date is nil.
I can't figure out how I should use the locale setting for my purpose.
The solution is to use getObjectValue:forString:range:error: method of NSDateFormatter and set the correct date and time style ad follow:
- (NSDate *)dateWithString:(NSString *)string locale:(NSString *)localeIdentifier {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
[formatter setLocale:locale];
[locale release];
NSDate *date = nil;
NSRange range = NSMakeRange(0, [string length]);
NSError *error = nil;
BOOL converted = NO;
converted = [formatter getObjectValue:&date forString:string range:&range error:&error];
[formatter release];
return converted? date : nil;
}
example:
NSString *italianDate = #"30/10/2010";
NSString *italianLocale = #"it_IT";
NSDate *date = [myCustomFormatter dateWithString:italianDate locale:italianLocale];
I struggled with German formats as well sometime ago and solved the problem by supplying my own formatting string:
[formatter setDateFormat:#"dd.MMM.yyyy HH:mm:ss"];
Then:
[dateFormatter dateFromString:#"01.Dez.2010 15:03:00"];
will get a correct NSDate.