Date with month name date, year in objective-c - objective-c

I have a date string formatted as "September 10, 2013". How can I convert this representation into a format such as "yyyy/mm/dd".
NSString *strDate = #"September 10, 2013";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df dateFromString:strDate];
NSDate *date = [df dateFromString:strDate];
[df setDateFormat:#"yyyy/mm/dd"];
NSString* temp = [[NSString alloc] init];
temp = [df stringFromDate:date];
NSLog(#"date i required %#", temp);
temp object is null here.
Thanks in advance.

Let's rework your original code, and discuss it as well:
NSString *strDate = #"September 10, 2013";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
For the input you're attempting to parse, you've got to provide a format string for your date formatter. If you don't, the date formatter is going to use defaults that are determined by the settings in your Date & Time control panel. Not only do these settings vary from locale to locale, they're subject to whatever changes the user may have effected. Another way of putting it is that they might not match the format of the date string you're attempting to convert.
You also don't want to rely on predefined styles such as NSDateFormatterShortStyle, NSDateFormatterMediumStyle or NSDateFormatterLongStyle. They're meant for date display (if you're willing to accept the Date & Time control panel settings), not for parsing.
There's a document you should consult, which is Unicode Technical Standard #35. Look in the table labeled "Date Field Symbol Table." Based on the information presented there (and your input), you'd set up your date converter to parse with a format string like this one:
[df setDateFormat:#"MMMM dd, yyyy"];
Now you can use your date formatter to parse your date string, and it'll work:
NSDate *date = [df dateFromString:strDate];
From this point onward, you're looking pretty good (though I've removed a superfluous line or two throughout all of this):
[df setDateFormat:#"yyyy/MM/dd"];
NSString *temp = [df stringFromDate:date];
NSLog(#"date i required %#", temp);
Best wishes to you in your endeavors.

You can use the NSDateFormatterclass -- look at the stringFromDate and dateFromString methods.
UPDATE: you have a couple of problems -- first, you need to tell the formatter what the initial format should be. Second, you have a format problem with the second format string -- 'm' is minutes, 'M' is months. You should review the documentation here. Here is an example:
NSString *strDate = #"September 10, 2013";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterLongStyle];
[df dateFromString:strDate];
NSDate *dat = [df dateFromString:strDate];
[df setDateFormat:#"yyyy/MM/dd"];
NSString* t = [df stringFromDate:dat];
NSLog(#"date i required %#", t);

Related

How do I get the alphanumeric abbreviated day of month in objective-c?

NSDate in Objective-c used to have dateWithNaturalLanguageString which accepted the use of abbreviated alphanumeric days of month with in strings like: #"Aug 2nd, 2010", but this method is deprecated, and I am trying to use NSDateFormatter instead:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy"];
but I can not use the following string with the above format:
NSDate date* = [dateFormatter dateFromString:#"Aug 2nd, 2010"];
since it will cause the date to be null due to incompatible format. I checked out the unicode standard date formats but I could not find anything that has an abbreviated alphanumeric day of month, and I am forced to use #"Aug 02, 2010" instead. But this is not desirable since I need abbreviated alphanumeric day of month both for setting a date from a string and getting a string from a date. After searching hours through various documentations I am out of ideas. Is there anyway other than the deprecated dateWithNaturalLanguageString? Or do I have to make a method of my own?
NSDateFormatter does not support ordinal suffixes (in English).
An alternative is to remove the suffix with Regular Expression
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.dateFormat = #"MMM dd, yyyy";
NSString *dateString = #"Aug 2nd, 2010";
NSString *trimmedString = [dateString stringByReplacingOccurrencesOfString:#"(\\d{1,2})(st|nd|rd|th)"
withString:#"$1"
options: NSRegularExpressionSearch
range:NSMakeRange(0, dateString.length)];
NSDate *date = [dateFormatter dateFromString:trimmedString];
NSLog(#"%#", date);

Date not displayed properly in the nib file

I have a string with value #"15/11/13". I need to display the same on the label in the nib file.
I am using the following code to display it
NSDateFormatter * df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"yyyy-mm-dd"];
[df1 setDateStyle:NSDateFormatterShortStyle];
[df1 setTimeStyle:NSDateFormatterNoStyle];
[profile1 setLastPromotionDate:[df1 dateFromString:#"11/11/13"]];
Profile1 is a different class which has lastPromotonDate of type NSDate.
In the nib file I have a outlet to display date which is bound to lastPromotionDate.
When I run the app, the date displayed is Monday, 11 November 2013 12:00:00 AM India Standard Time.
Can I know what is the mistake here? What has to be done so the date displays in this format : 11/11/13
Try this:-
NSString *dateStr=#"11/11/13";
NSDateFormatter *format=[[[NSDateFormatter alloc]init]autorelease];
[format setDateFormat:#"dd/mm/yy"];
NSDate *dt=[format dateFromString:dateStr];
NSString *str=[format stringFromDate:dt];
NSLog(#"%#",str);
NSString *str = #"15/11/13";
// here we create NSDateFormatter object for change the Format of date..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
// here set format of date which is in your output date (means above str with format)
[dateFormatter setDateFormat:#"yy/mm/dd"];
// here you can fetch date from string with define format
NSDate *date = [dateFormatter dateFromString: str];
dateFormatter = [[NSDateFormatter alloc] init] ;
// here set format which you want..
[dateFormatter setDateFormat:#"dd/mm/yy"];
NSString *convertedString = [dateFormatter stringFromDate:date];
//here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
rofile1.text = convertedString;
Why not just set it as:
NSString *str = #"15/11/13";
[label setStringValue: str];
UPDATE:
To convert this string to NSDate of the same format use:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/mm/yy"];
NSDate * date = [df dateFromString:str];
To get back the string value of date from NSDate use:
NSString *dateStr = [df stringFromDate: date];
Also you may use this dateStr to set you label text as
[label setStringValue: dateStr];
or bind the label to "dateStr". (Do not bind it to "date" of NSDate type, I think this is where you are going wrong).
Use "date" variable that is of NSDate type for your server requests.
UPDATE:
Since you are binding your label to NSDate value, it displays the complete date in the way it is present in NSDate. To retrieve the value of that NSDate in your custom format, you need to use NSDateFormatter that will write the part of NSDate that we need in our format to a NSString.
Also if we convert a NSString to NSDate, it doesn't mean that date formatter will save the NSDate in our custom format, the format of the date formatter specify the format of our string so that NSDate could read the correct date from our custom formatted string. But NSDate will always save the date value in its own format.
We have something called DateFormatter under objects in the library. We can drag and drop that under the text cell if the label in the list view of the nib.
This data formatter converts the date format to string value which is used to display in the UI.

Objective-C – NSDateFormatter dateFromString ignore time

If I'm not interested in the time can I ignore it? I.e I have a date string that looks like this #"2012-12-19T14:00:00" but I'm only interested in getting the date (2012-12-19) but if I set NSDateFormatter like [dateFormatter setDateFormat:#"yyyy-MM-dd"]; it will return me a nil NSDate.
An NSDate object will always contain a time component as well, as it is representing a point in time — from this perspective one could argue the name NSDate is misleading.
You should create a date formatter for creating dates from string, set the time to the start of the day and use a second date formatter to output the date without time component.
NSString *dateString = #"2012-12-19T14:00:00";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateStyle:NSDateFormatterShortStyle];
[outputFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [inputFormatter dateFromString:dateString];
//this will set date's time components to 00:00
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit
startDate:&date
interval:NULL
forDate:date];
NSString *outputString = [outputFormatter stringFromDate:date];
NSLog(#"%#", outputString);
results in
19.12.12
while the format — as it is chosen by styling — will be dependent of your environment locale
all date string returns 10 characters for the date, what i mean is the date of todayy will be 2012-11-19
you can easily substring the date and use it as you want:
Example :
NSString* newDate = #"";
newDate = [[NSDate date]substringToIndex:10];
the out put will be : 2012-11-19

Convert Short Zulu format date to NSDate

I am having troubles converting a short Zulu date format to a NSDate Object. I have found some answers for converting Zulu strings but mine looks like:
20111210T1000
And based on my researches, I am trying to do:
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyyMMdd'T'HHmmss Z"];
NSDate *date = [f dateFromString:[str stringByReplacingOccurrencesOfString:#"Z" withString:#" +0000"]];
[f release];
I've tried many ways but my date is still nil...
How should I set my NSDateFormatter?
Here is a quick fix, in your date string you miss the timezone in the format, you should append (Paris one here) to your string and it should work. Also the format was wrong.
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyyMMdd'T'SSSZ"];
NSString* str = #"20111210T1000-0100"; // NOTE -0100, GMT +1 Paris zone
NSDate* date = [df dateFromString:str];
NSLog(#"%#", date);

How do I convert from NSString to NSDate using NSDateFormater

I have this string: 2012-01-12T21:01:00 and this code:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate* arrivalTime=[df dateFromString:field_arrival_time];
But it returns nil. What date format should I use to parse this string?
Your string 2012-01-12T21:01:00 contains the literal T (At least I believe it's a literal, it doesn't appear to signify a timezone). You must include this in your date format.
[df setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss"];
Note the lack of the a in the date format, using it will require your input string to use AM or PM preceded by a white space. For more information on special characters with NSDateFormatter take a look at the Date Formatting Guide paying extra attention to the Fixed Formats.
Edit: Your input string does not specify a timezone, it will probably be interpreted as UTC and be localized to the timezone of your machine when you output it through NSLog().
I have tried a lot on your string but never get result. only nil shows on console. but when i remove character "T" from your string it gives perfect result.
[f setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [f dateFromString:#"2012-01-12 21:01:00"];
NSLog(#"date %#", date);
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-yyyy"];
NSString *s = [f2 stringFromDate:date];
NSLog(#"S %#", s);
/// OutPut : S 12-01-2012