issue in Changing date formate - iphone

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

Related

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

Date format does not work properly

I take date into string. date format is like this and string is
datenotification=20-02-2012
Now I want to change this into
20-feb-2012
but for checking purposes I just place that into a dateformatter and print that but it shows me an incorrect date
NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"dd-MM-YYYY"];
NSDate *d=[currentdateformate dateFromString:dateNotification];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];
Hey Check this example
NSString *string = #"12-02-2000";
NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"dd-MM-yyyy"];
NSDate *d=[currentdateformate dateFromString:string];
[currentdateformate setDateFormat:#"dd-MMM-yyyy"];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];
NSLog(#"S %#",str3);
output S 12-feb-2012

How display section of time in different text field?

I have three text field in which i want to show the current time. In the first text field,it shows only hours , in second text field it shows only minute and in last one it shows am or pm. For getting current time I use this code but how do I show according to my requirement?
code...
- (void)viewDidLoad {
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"HH:MM"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(#"%#",str);
//Set in the lable
time_label.text=[NSString stringWithFormat:#"%# AM",str];
[super viewDidLoad];
}
Thanks in advance...
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"hh MM a"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(#"%#",str);
NSMutableArray *Arr=[str componentsSeparatedByString:#" "];
textFeild1.text=[Arr objectAtIndex:0]; //hoursTextFeild
textFeild2.text=[Arr objectAtIndex:1]; //minutesTextFeild
textFeild3.text=[Arr objectAtIndex:2];//Am/Pm TextFeild
- (void)viewDidLoad {
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"HH:MM"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(#"%#",str);
//Set in the label
//seperate its components
NSMutableArray *array = [str componentsSeperatedByString:#" "];
[firstTextField setText:[array objectAtIndex:0]];
[secondTextField setText:[array objectAtIndex:1]];
[thirdTextField setText:#"AM/PM"];
[super viewDidLoad];
}

NSDate from NSString returning null problem

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.

NSDateFormater from NSMutableArray value

I store some dates in my NSMutableArray, I want to retrieve them and change the format.
For the moment I do this:
NSDate *myDate = [self.dates objectAtIndex:0];
NSLog(#"myDate:%#", myDate); // 2010-03-02
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:#"MMMM d YYYY"];
NSString *newDate = [formatDate stringFromDate:myDate];
NSLog(#"newDate: %#", newDate); // NULL
the result for newDate is NULL, and I want it to be something like that: "March 3, 2010"
thanks,
That works for me, but I replaced the first line with:
NSDate *myDate = [NSDate date];
My output:
2010-03-02 19:29:08.045 app[11464:a0f] myDate:2010-03-02 19:29:08 -0800
2010-03-02 19:29:08.048 app[11464:a0f] newDate: March 2 2010
My best guess is that [self.dates objectAtIndex:0] isn't returning what you want it to.
Since your dates array contains strings, you have to convert the string to an NSDate and then you can re-convert the date to the format you want.
NSDateFormatter *strToDateFmt = [[NSDateFormatter alloc] init];
[strToDateFmt setDateFormat:#"yyyy-MM-dd"];
NSDate *myDate = [strToDateFmt dateFromString:[self.dates objectAtIndex:0]];
[strToDateFmt release];
NSLog(#"myDate:%#", myDate);
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:#"MMMM d YYYY"];
NSString *newDate = [formatDate stringFromDate:myDate];
[formatDate release];
NSLog(#"newDate: %#", newDate);