Dateformatter change the year of my date - objective-c

I try to display a date with a dateformatter but this one change the year of the date.
This is the method :
setLabelWithDate:(NSdate *)date{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"dd MMMM YYYY"];
NSLog(#"Date : %#",date);
NSLog(#"stringFromDate %#",[dateFormatter stringFromDate:date]);
self.selectedDateLabel.text = [dateFormatter stringFromDate:date];
[dateFormatter release];
}
And this is what is displayed in the console :
2012-05-04 13:08:50.442 MyAppli[3339:fb03] Date : 2012-12-30 00:00:00 +0000
2012-05-04 13:08:50.443 MyAppli[3339:fb03] stringFromDate 30 December 2013
Here I m. The dateFormatter add myDate one year and I really don't know why. This case happens only for the 2 last dates of the year (30 and 31 december).
Thanks for your help.
Alexandre

Use
[dateFormatter setDateFormat:#"dd MMMM yyyy"]; // lowercase y
and you probably should check this and this.

Related

Difference between dateFromString and stringFromDate for this pattern 'YYYY'

want to understand the Difference between the Difference between dateFromString and stringFromDate for this pattern "YYYY".
Because I have written the below logics for dateFromString and stringFromDate but result is different like Mon Dec 30 09:05:00 2019 and 2020/12/30 09:05
// convert string to date
NSString *dateStr = #"2019-12-30 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm"];
//NSDate *date = [dateFormat dateFromString:dateStr];
NSDate *dateValue = [dateFormat dateFromString:dateStr];
NSLog(#"string to date == %#",dateValue);
// convert date to string
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[df1 setLocale:locale];
[df1 setDateFormat:#"YYYY/MM/dd HH:mm"];
NSString *datestr = [df1 stringFromDate:dateValue];
NSLog(#"date to string == %#", datestr);
so out put is below
string to date == Mon Dec 30 09:05:00 2019
date to string == 2020/12/30 09:05
Both or same pattern YYYY It is working as a week based calander. but why year value 2019 and 2020 is differing when using following methods dateFromString and stringFromDate?
When you use yyyy, you will get the calendar numeric year.
The YYYY you will get the week of year, that can do this boundary rollover that you are experiencing. Read the docs at: http://www.unicode.org/reports/tr35/tr35-dates.html#dfst-year
Always when I have problems with date formatters I usually go to this website https://nsdateformatter.com/.
The week number of 2019-12-30 is week 1 of 2020. The year of week 1 of 2020 is 2020. Try format yyyy/MM/dd HH:mm YYYY w (week of year) the result is 2019/12/30 09:05 2020 1.

Set date format Objective-C

I want to convert this string "Sat, 01 Aug 2015 21:03:59 GMT" to NSDate object
Here's my code
+(NSDate *)getDateFromDateString :(NSString *)dateString {
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"E, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
return date;}
but date always is nil. I guess something wrong with date format. Can someone please give me some advice?
Thanks for your support, i found out the answer. Need to declare the locale
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
Normally this is how I would call a utility function:
DateHelperClass.h
+ (NSDate *)getDateFromDateString :(NSString *)dateString;
DateHelperClass.m
+ (NSDate *)getDateFromDateString :(NSString *)dateString
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
return date;
}
SomeOtherClass.m
#import "DateHelperClass.h"
- (void)convertDate
{
NSLog(#"%#",[DateHelperClass getDateFromDateString:#"Sat, 01 Aug 2015 21:03:59 GMT"]);
}
Result:
2015-08-01 21:03:59 +0000

NSDateFormatter dateFromString with date

i have a problem with method dateFromString, here is my code
NSString* res = [NSString stringWithFormat:#"%#/%#/%#",dateInput.text,monthInput.text,yearInput.text];
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:#"dd/MM/yy"];
NSDate* inpTime = [formatter dateFromString:res];
[dateResult setText:[NSString stringWithFormat:#"%#",inpTime]];
when I run, the date in "inpTime" always is "dateInput" - 1.
for example: if "dateInput" is 5, the date in "inpTime" will be 4
You need to adjust the timezone.
Change
[formatter setTimeZone:[NSTimeZone localTimeZone]];
to
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
This is because you're not setting the time, so it's set by default the midnight UTC.
But when you are displaying the date with a timezone other than UTC the time is shifted accordingly.
As an example if you live in New York the 12/29/2012 00:00:00 UTC is actually the 12/28/2012 18:00:00 for you.
You Code is perfect, no error what so ever.
Try nslogging dateInput.text, monthInput.text and yearInput.text...might be from here you are getting invalid data.

How to get a local formatted date patterned string in iOS? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to determine if locale's date format is Month/Day or Day/Month?
I've struggled some time now and I'm stuck! I either need a local formatted pattern date string in the form "mm-dd-yyyy" or "08-25-2012". How should I do this? Use dateFormat? Please help me!
NSDateFormatter has a convenience class method:
[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle];
You can specify NSDateFormatterNoStyle,..ShortStyle,..MediumStyle, or..LongStyle
You can use NSDateFormatter with MM-dd-yyyy and then use NSTimeZone applied to the formatter to adjust for the local time zone.
This:
NSDate *date = [[NSDate alloc] init];
NSLog(#"%#", date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy hh:mm"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"%#", dateString);
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
NSLog(#"adjusted for timezone: %#", [dateFormatter stringFromDate:date]);
Outputs:
2012-08-26 08:30:53.741 Craplet[2585:707] 2012-08-26 12:30:53 +0000
2012-08-26 08:30:53.742 Craplet[2585:707] 08-26-2012 08:30
2012-08-26 08:30:53.743 Craplet[2585:707] adjusted for timezone: 08-26-2012 08:30

Conversion NSString to NSDate. 1 day is lost

NSString *dateString = #"20.10.2010";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSLog(#"%#", [dateFormatter dateFromString:dateString]);
My output is:
2010-10-19 22:00:00 GMT
Why is one day lost?
Probably because your locale specificies that you're in GMT +2.
That means the given date is interpreted as 2010-10-20 00:00 GMT+2, hence in GMT+0 that's 2010-10-19 22:00.
You not lost 1 day, but 2 hours. But the display is GMT.
What do you want to do with your date ?
See the reference to change the output formatter