Date formatting not working for 5/1916 - ios

I'm trying to format two NSString to an NSDate with this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/yyyy"];
NSString *dateString = #"5/1916";
NSString *dateString2 = #"6/1916";
NSDate *date = [dateFormatter dateFromString:dateString];
NSDate *date2 = [dateFormatter dateFromString:dateString2];
Guess what:
date is nil and date2 works fine (isn't nil).
Any idea how this come?
Edit:
For those who think it's about the string, the next screenshot has the samen problem.

your code works perfectly fine with following result
Printing description of dateString:
05/1916
Printing description of date:
1916-04-30 18:06:40 +0000
But issue is that NSDate is something which must have day, month , year Just revamp your code to feed dateformatter a correct date.

in most languages, 5/1916 wouldn't be accepted, but 05/1916 is, since the format is MM/yyyy not M/yyyy. the length of string is mandatory.
hope that helps,
eiran

Related

Problems converting a string date to NSDate

I have this string date:
2014-04-21T07:55:13Z
when I convert that to NSDate I have the hour like 6:55... 1 hours less. WHY?
This is the code I am using to convert:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *newDate = [dateFormatter dateFromString:dateStr];
newDate is now 2014-04-21 06:55:13 +0000 !!!???
what is wrong?
NOTE: That one hour less would make sense if the date was my local time (GMT+1) being converted to GMT. But if that Z is zero offset ( = GMT) the date is already GMT.
I don't think your code is wrong. using this code:-
NSString *dateStr = #"2014-04-21T07:55:13Z";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#" date log %#",date); //2014-04-21 02:25:13 +0000 output
// Convert date object to desired output format
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"string %#",dateStr); //2014-04-21T07:55:13Z output
but NSLog of NSDATE is not output correct according to this NSDate Format outputting wrong date so your code is right.
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.
NSLog(#" date is %#",[dateFormat stringFromDate:date]);

Check NSString for specific date format

I have a NSString and I need to check that it is in a this specific format MM/DD/YY. I then need to convert that to a NSDate. Any help on this would be much appreciated. Sidenote - I have searched around and people suggest using RegEx, I have never used this and am unclear about it generally. Can anyone point me to a good resource/explanation.
NSString *strDate1 = #"02/09/13";
NSString *strDate2 = #"0123/234/234";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *dateFormat1 = [dateFormatter dateFromString:strDate1];
NSDate *dateFormat2 = [dateFormatter dateFromString:strDate2];
NSLog(#"%#", dateFormat1); // prints 2013-09-02 00:00:00 +0000
NSLog(#"%#", dateFormat2); // prints (null)
So you will know when it's not formatted correctly if the NSDate is nil. Here's the link to the docs if you need more info: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
Use an NSDateFormatter for both tasks. If you can convert the string to a date then it is in the correct format (and you already have the result).
I know that this is a late answer, but it is impossible to always guarantee that a string is in this particular date format.
A date formatter, a regex, or even a human can not verify certain dates, because we don't know if the user is entering "mm/DD/yy" or "DD/mm/yy". It is common in some places to enter the day of the month first, while in other areas you enter the month first. So if they enter "09/06/2013" do they mean "September 6th" or the "9th of June"?
Here is a simple function for anyone searching for a simple solution.
- (BOOL) isTheStringDate: (NSString*) theString
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:theString];
if (dateFromString !=nil) {
return true;
}
else {
return false;
}
}
You have to change the formatter below to match the formatting your date is using.
[dateFormatter setDateFormat:#"yyyy-MM-dd"];

How do I NSLog an NSDate?

With the code pasted below, I am trying to log an NSDate. What am I doing wrong here?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd"];
NSDate *todaysDate;
todaysDate = [NSDate date];
NSLog(#"Todays date is %#",formatter);
All you have to do is:
NSLog(#"Todays date is %#",[formatter stringFromDate:todaysDate]);
What you are doing wrong is you haven't done anything to associate the date with the formatter. So, you would want to do something like this:
NSLog(#"Todays date is %#", [formatter stringFromDate:todaysDate];
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: to actually format the date for pretty human-readable display.
If what you need is to just see the date information and don't need a particular format, you don't need a formatter to log a date:
NSLog(#"Todays date is %#", todaysDate);
Will work fine to give you the -description of the NSDate object. I wouldn't use this for anything you display to the user (do use an NSDateFormatter for that), but this is handy if you're just debugging and need to see information about an NSDate object.
Complete example:
NSDate *today = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd"];
NSLog(#"%#",[formatter stringFromDate:today]);
[formatter release];

NSDateFormatter won't format strange date string

So I have a date string I receive that looks like this: "2013-03-20T21:13:26-7:00" that I receive from a web back end. I have no control over the back end, just a fyi.
My preference would be to have the date formatted like this: 9:13pm at 3/20.
When I do the following
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date = [dateFormatter dateFromString:#"2013-03-20T21:13:26-7:00"];
date is null.
My first thought is that the date string looks odd, and maybe I should remove the T and the "-7:00", as the "-7:00" is appended to every date I receive, and I'm not sure what it is for.
Even after the string looks like #"2013-03-20 21:13:26", date is still null.
I will admit I am not a pro at formatting dates, so if I could get some help with this issue, that would be great.
Thanks in advance!
You have to set dateFormat to the dateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
NSDate *date = [dateFormatter dateFromString:#"2013-03-20T21:13:26-7:00"];
[dateFormatter setDateFormat:#"hh:mma 'at' MM/yy"];
NSString *dateString = [dateFormatter stringFromDate:date];
Set the date format for the dateFormatter, your problem lies in the last part of the date, secondly you can set the T in the dateformatter as follows
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *date = [dateFormatter dateFromString:#"2013-03-20T21:13:26-7:00"];
Set dateFormat,
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];

Convert date from mysql to NSDate

I have a varchar in mysql that holds dates. I have been trying to convert it into an NSDate, but nothing I have tried yet works. The NSString from mysql looks like this:
August 11, 2012, 10:17 AM
All the posts so far relating to NSString to NSDate conversions have not yet worked for this string. If someone could please help...
I assume you use JSON for getting data from MySQL(server) and have a JSON object called myJSONObject
NOTE: myJSONObject must be serialized using some framework like NSJSONSerialization
EDIT with some detailed question links
You have asked to convert data to August 11, 2012, 10:17 AM but my sample code tries to convert as 2012-08-11 10:17:.., i edited my sample code for you. For more, take a look at NSDateFormatter
ATTENTION PLEASE: you can get the variable month_from_date using NSDateComponents. There are some questions about it here and here.. If you can't, please open a new post.
NSString* dateString = [myJSONObject objectForKey:#"date"];
NSDateFormatter* fmt = [NSDateFormatter new];
[fmt setTimeZone:[NSTimeZone defaultTimeZone]];
// [fmt setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[fmt setDateFormat:#"%# DD,YYYY, HH:mm",month_from_date];
NSDate* dateFromString = [fmt dateFromString:dateString];
NSLog("Here is date from string: %#",dateFromString);
to convert a NSString into a NSDate, please, use the NSDateFormatter.
NSString *_dateString = #"August 11, 2012, 10:17 AM";
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:#"MMMM dd, yyyy, HH:mm a"];
NSDate *_date = [_dateFormatter dateFromString:_dateString];
NSLog(#"raw date : %#", _date);
NSLog(#"formatted date : %#", [_dateFormatter stringFromDate:_date]);
the output NSDate will contains the date:
raw date : 2012-08-10 23:17:00 +0000
formatted date : August 11, 2012, 11:17 AM
you can format the NSDate as you'd like for a different output, it is up to up now, but you can work with the NSDate object which contains the date.
NOTE: we don't know the timezone from the input date when we are converting it, so the NSDate using the defaults for it. if you set the current timezone you will get the correct date after conversion.

Resources