Difference between dateFromString and stringFromDate for this pattern 'YYYY' - objective-c

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.

Related

Custom date string to NSDate

I am trying to convert Mon, 01 Aug 2016 04:15 PM IST to NSDate
I tried using the below code but always stuck with nil. Please help
NSString *fullTime = #"Mon, 01 Aug 2016 04:15 PM IST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a ZZZ"];
NSDate *tempTime = [dateFormatter dateFromString:fullTime];
You main issue is the IST in the date, IST is not a standard and can stand for many time zones:
IST Indian Standard Time UTC+05:30
IST Irish Standard Time UTC+01
IST Israel Standard Time UTC+02
The date formatter will not be able to correctly format the date since it does not know which timezone is meant.
If you can you should have the date changed or remove the IST part from the date.
Also you will need to add a locale to the date formatter you it knows in which language is used in the date string.
For english beste use en_US_POSIX:
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
I tried your code.First it gives nil.Then I changed LocaleIdentifier to en_IN because
IST stands for both Israel Standard Time, and India Standard Time also it indicates this
NSString *strDate = #"Mon, 01 Aug 2016 04:15 PM IST";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm a zzz"];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"];
NSDate *dateStr = [dateFormat dateFromString:strDate];
NSLog(#"The date is - %#",dateStr);
The Printed Result is
The date is - 2016-08-01 06:45:00 +0000
Convert between date formats in Objective-C

DateFormatter keep returning null

I have a date i take from twitter :
NSString *date=[twt objectForKey:#"created_at"];
NSLog(#"%#",date);
NSDateFormatter *df = [ [NSDateFormatter alloc] init] ;
[df setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSDate *newdate = [df dateFromString:date];
NSLog(#"new: %#",newdate); //null
the date is : Thu Jun 12 20:56:53 +0000 2014 and i get null on the new date .
What am i missing ?
the date formatter for that format (Thu Jun 12 20:56:53 +0000 2014) would be this:
"EEE MMM d HH:mm:ss Z yyyy"
rather than this "dd-MM-yyyy HH:mm:ss".
The date formatting string you've shared sure does't look like it matches the date you're getting from Twitter. The docs for dateFromString state...
A date representation of string interpreted using the receiver’s
current settings. If dateFromString: can not parse the string, returns
nil.
Read up!
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

NSDate coming null in iOS

I have to get a date from a string. So I am using the below code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Fri Mar 09 15:31:35 CST 2012
[formatter setDateFormat:#"EEE MMM d HH:mm:ss ZZZ yyyy"];
NSDate *date = [formatter dateFromString:[newDict objectForKey:#"eventDate"]];
NSLog(#"aDate is %#",[newDict objectForKey:#"eventDate"]);
eventDate is coming in the format aDate is Fri May 18 12:00:37 IST 2012 but date is coming null.
I know it is a small issue but I am really stuck up with it.
Your input string can be parsed with a format of #"EEE MMM d HH:mm:ss VVV yyyy".
Try adding this before the dateFromString: call:
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"]];

Dateformatter change the year of my date

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.

NSDateFormatter return incorrect date from string

I have a method,
+ (NSDate *) convertToDateFrom:(NSString *) dateString
{
if (dateString == nil || [dateString isEqual:#""]) return nil; //return nil if dateString is empty
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMMM yyyy HH:mm"];
NSDate *date = [df dateFromString:dateString];
return date;
}
When I pass,
#"Monday, 21 November 2011 17:01" //Passed string
It returns a wrong date,
2011-11-21 23:14:00 +0000 // Output
I am not sure whether I am using those flags correctly or NSDateFormatter isn't properly converting my string to date.
Am I doing something wrong?
Thanks
The +0000 at the end of the date indicates GMT. All dates are stored relative to GMT; when you convert a date to a string or vice versa using a date formatter, the offset to your time zone is included. You can use NSDateFormatter's -setTimeZone: method to set the time zone used.
In short, you're not doing anything wrong in your code. Use [df stringFromDate:date]; to see that the date is correct. (You can also use NSDate's -descriptionWithCalendarFormat:timeZone:locale:.)
try using
df stringFromDate:date
Following worked on mine,
NSLog(#"Date for locale %#: %#",
[[dateFormatter locale] localeIdentifier], [df stringFromDate:date]);
gave me output as :
Date for locale en_US: Wednesday, 26 June 2013 15:50
Try setting the time zone and locale.
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];