Wrong year set when converting NSDate to NSString - string

I'm trying to produce a string using a NSDate category in this way:
NSString* dateString = nil;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocal currentLocale]];
[dateFormat setDateFormat:#"dd LLL YYYY"];
dateString = [dateFormat stringFromDate:self];
return dateString;
The conversion works fine except in ONE case (I report the debug session):
if I try to convert an NSDate object like this:
(gdb) po self
2012-01-01 00:00:00 +0000
I obtain:
(gdb) po dateString
01 Jan 2011
Why the year is set back to 2011????
PS. I have already checked NSDate returns wrong year and I'm NOT using the Japanese calendar.
thanks a lot

Try this:
NSDate *pickerDate = [NSDate date];
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease];
components.day = 0; //This value to take from today to next 1 or 2 or 3 days
NSDate* newDate = [calendar dateByAddingComponents: components toDate: pickerDate options: 0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMMM"];
NSString *textDate = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:newDate]];
[dateFormatter release];

Related

how to convert date to string like this "2013-12-23T12:02:01+05:30"

I have string "2013-12-23T12:02:01+05:30"
i want to convert into date but i am getting NSDate nil
here is my code
// Convert string to date object
NSString *dateStr = #"2013-12-23T12:02:01+05:30";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy, MM DD 'T' HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
Your string format does not match you date string:
NSString *dateStr = #"2013-12-23T12:02:01+05:30";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
In you example date sting there are - used in the date, in the format you are using uses . Also DD will give you the day in the year not in the month. and there is no space after the seconds and time zone offset.
You need to set the correct date format to match with the date string
NSString *dateStr = #"2013-12-23T12:02:01+05:30";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormat dateFromString:dateStr];

NSTimeInterval since1970 to NSDate

I have the following code:
-(void)setDate:(double)dateInterval {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)dateInterval];
NSLog(#"NSDate-Result: %#", [date description]);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setDateFormat:#"YYYY-MM-DD HH:mm:SS"];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"String-Result: %#", dateString);
date_label.text = dateString;
}
in NSDate the Date is correct, but the converted string isn't correct anymore.
Results:
2012-08-03 10:58:40.469 iSkizzenaufmass[4148:c07] NSDate-Result:
2012-08-03 08:08:35 +0000 2012-08-03 10:58:40.471
iSkizzenaufmass[4148:c07] String-Result: 2012-08-216 10:08:48
You have to use yyyy-MM-dd HH:mm:SS instead of -DD (DD is day of the year)
EDIT: changed YYYY to yyyy as YYYY is week based year and could differ from current year (see link below for all opts)
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

NSDateFormatter Convert String To Date Always Military Time

I have a string which represents a date stored in military time. I want to display this string in a label in 12 hr time. Here is my code snippet:
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:timeZone];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss"];
NSLog(#"Current Date: %#", [dateFormat stringFromDate:[NSDate date]]);
NSLog(#"Sent date: %#",[myPlanData valueForKey:#"planDate"]);
NSDate *aDate =[dateFormat dateFromString:[myPlanData valueForKey:#"planDate"]];
NSLog(#"Converted date is: %#",aDate);
NSString *planDateString = [dateFormat stringFromDate:aDate];
NSLog(#"The converted date string is: %#",planDateString);
planDateLabel.text=planDateString;
The output is:
Current Date: 06/28/2012 10:08:48 - (so my date formatter appears correct?)
Sent date: 06/30/2012 20:47:34 - (this is the value being sent)
Converted date is: (null) - (Here is where it breaks!)
The converted date string is: (null)
If i change my dateformat to
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
Everything goes smooth but I end up with Military time being displayed. I simply want to convet that to 12 hr time and display in a label
Well here is how i ended up fixing it
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:timeZone];
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSDateFormatter *dateFormatback = [[NSDateFormatter alloc] init];
[dateFormatback setTimeZone:timeZone];
[dateFormatback setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSLog(#"Current Date: %#", [dateFormat stringFromDate:[NSDate date]]);
NSLog(#"Sent date: %#",[myPlanData valueForKey:#"planDate"]);
NSDate *aDate =[dateFormat dateFromString:[myPlanData valueForKey:#"planDate"]];
NSLog(#"Converted date is: %#",aDate);
NSString *planDateString = [dateFormatback stringFromDate:aDate];
NSLog(#"The converted date string is: %#",planDateString);
planDateLabel.text=planDateString;
Not sure if this is the best or right way to do it but it works!

NSDateFormatter gives different output/wrong (GMT?) time

I have tried setting the timezone and locale of the NSDateFormatter but I can't seem to get anything to work. Here is the code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss"];
NSString *myFireDateString=[#"9/17/11" stringByAppendingString:#" 09:00:00"];
NSDate *myFireDate = [dateFormat dateFromString:myFireDateString];
NSLog(#"The datestring is is %#",myFireDateString);
NSLog(#"The formatted date is %#",myFireDate);
Here is the output:
The datestring is is 9/17/11 09:00:00
The formatted date is 2011-09-17 13:00:00
You need to set a timezone to your dateFormatter:
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];
See NSDate from NSString , shows the previous day

How to Convert XML Date format to Cocoa

I have a Date format:
2009-08-10T16:03:03Z
that I want to convert to:
#"MMM dd, HH:mm a"
I retrieve the xml format in an NSString. I tried to use the NSDateformatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd, HH:mm a"];
any ideas?
NSString *yourXMLDate = #"2009-08-10T16:03:03Z"
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MMM dd, HH:mm a"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
Hope this helps!
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700
NSDate *date = [dateFormatter dateFromString:self.currentDate];
[item setObject:date forKey:#"date"];
[items addObject:[item copy]];
here edit the following code
[dateFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700
to your required Format.

Resources