stringFromDate and dateFromString sometimes return null - ios

I simply have to format the date which comes in this format "2013:07:24 11:05:04" into this format "24-lug-2013", this is the code that i use for do this:
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"yyyy:MM:dd hh:mm:ss"];
NSDate *d = [dtF dateFromString:self.creationDate];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:#"d-MMM-yyyy"];
NSString *strDate = [dateFormatStr stringFromDate:d];
NSLog(#"%#",strDate);
This code works right, the problem is that after a few call to this method, sometimes i get a null string, but the format of the input is still the same.
For example with this string as an input "2013:07:24 11:05:04" i get this: 24-lug-2013, which is exactly what i want, but after 7 call in this case, with this input "2013:07:24 13:47:31" i get a null NSDate and consequentially the result string in null.
where is the problem??
Thanks

Date formatters return nil when the date string that is passed into them does not match the format. The code you've posted uses dateFromString on an input string. If that fails, then your NSDate "d" will be nil, and so the call to stringFromDate will likely also return nil. (Although stringFromDate might also crash if you pass in an NSDate of nil)

i solved by change this line :
[dtF setDateFormat:#"yyyy:MM:dd hh:mm:ss"];
with this:
[dtF setDateFormat:#"yyyy:MM:dd HH:mm:ss"];
this is because "hh" for hours, meaning values between 1 and 12 -- 14:00:00, eg, will fail. So If you want 24-hour format use "HH"

Related

Convert a date to English format form Bengali

I have a date picker in my app. The phone is set to Bangladesh local settings. When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.
Like, it returns ০৬/১১/২০১৪
but I want it to be 06/11/2014.
I've tried converting it by date formatter. This is what I tried:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
[formater setDateFormat:#"dd/MM/yyyy"];
NSLog(#"%#",[formater stringFromDate:date]);
The output is null.
You are incorrect in your assumption when you say...
When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.
UIDatePicker returns an NSDate object. NSDate has no formatting at all. It has no language, it is purely a point in time.
When you do this...
NSLog(#"%#", someDate);
The system will render that point in time into a string and then print it. It is the rendering into a string that contains the format.
I'm guessing what you are doing is this...
Get a date from a UIDatePicker.
Render that date into a UITextField in Bengali. (or label, or text view or something)
Trying to read the text and store it into a date.
Trying to then "convert" the date to an English string.
What you should be doing is just saving the date that comes from the date picker.
Put it into a property or something.
In your above code I think the bit that is failing is actually the first bit...
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];
Because you're not giving it a format it will fail. But this is the wrong way to go about it anyway.
You should have something like this...
- (void)datePickerChoseADate
{
self.date = self.datePicker.date;
}

Correct Format for String to Date?

I m sorry stackoverflow is full of dateformatter questions but i m having a really hard time to get a string into a NSDate.
This is the String i receive:
2014-12-22T06:49:40+0000
And this is how i m trying to format it and get it to a NSDate:
NSString *time = #"2014-12-22T06:49:40+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate = [df dateFromString:time];
Unfortunately myDate is always nil.
Am i missing somtheing or is my formatting simply wrong?
Thanks in advance
Try this:"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSSSSSZ"
You've wrapped your final Z in single quotes, which means you want it to be a literal Z character, but no such character exists in your time string. Removing the single quotes means it would be looking for a timezone offset, like you want.

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

Make NSDate Equal to NSString

I have a string:
stringValue = 2013-06-11 06:01:28.
When i try to convert it using NSDateFormatter it becomes like this: date = 2013-06-10 22:01:28 +0000.
I've read that they are the same point in time. In fact when i get the string value of the date, i get the string above.
If they're the same, is there a way to have a date with value equal to my string above? Can i have an NSDate *date = 2013-06-11 06:01:28? If yes, how can I do that?
It happen because when you convert string to date it convert to GMT time format
if you want date same as your string value than you have to set your stadard local time
Like
NSDate *date=[[NSDate alloc]init];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
[timeFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss'"];
date = [timeFormat dateFromString:#"2013-06-11 06:01:28"];
hope it may help you
Use this formatter:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];

NSDate with NSDateFormatter returns nil date

I want to convert a string to an NSDate, like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterFullStyle];
NSDate *createdAt = [dateFormat dateFromString:[value valueForKey:#"created_at"]];
When I log createdAt it returns nil for some reason and I can't figure out why.
[value valueForKey:#"created_at"]];
Returns a NSCFString that looks like this:
2013-05-15T05:55:57Z
I have no idea why it's nil, any help would be appreciated!
You should set date format to the NSDateFormatter first. This should be working:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *createdAt = [dateFormat dateFromString:[value valueForKey:#"created_at"]];
However I'm not sure about your input date string. The last letter Z should be identification of date's time zone. Letter Z is the format specifier for the time zone according to Unicode Date Format Patterns (http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns).

Resources