NSDateFormatter not working to covert NSString to NSDate - nsdateformatter

I am using NSDateFormatter to get a date from an NSString I am confused as to why the following is not working:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: timeDateUpdated];
myDate remains nil and yet, as the image shows the timeDateUpdated appears to be of the correct format.

Wrong format, use HH instead of hh.
The h directive is for hour in the am/pm format, will not work with 17.
See this comprehensive date formatting guide

Related

Get current NSDate with a format of: dd/MM/yyyy

I'm trying to get the current date with the following format: dd/MM/yyyy.
The way I would format it, is like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
And I would get the current date like this: [NSDate date]. How can I mix the 2 together, and get the current date with that format?
If you want to get current day like format this: dd/MM/yyyy.
You can use this code:
NSDate *datecenter = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d, MMMM, YYYY"];
NSString *dateString = [dateFormat stringFromDate:datecenter];
lbldaymonthyear.text = dateString;
This is probably the most succinct way, you can use different NSDateFormatterStyles to change the way the date is displayed later
NSString *date = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterNoStyle];
NSDateFormatterShortStyle
Specifies a short style, typically numeric only, such as “11/23/37” or “3:30 PM”.
Equal to kCFDateFormatterShortStyle.
More styles here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/c/tdef/NSDateFormatterStyle
You can use this code:
NSDate *datecenter = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd, MMMM, YYYY"];
NSString *dateString = [dateFormat stringFromDate:datecenter];
yourlabel.text = dateString;

Time formats for NSDate

I have datetime values coming into my app from a web service. The format is as follows:
2013-03-24T09:45:00.000-04:00
I need only the time from this value. I am using NSDateFormatter and am setting the date format as follows:
[df setDateFormat:#"HH:mm:ss"];
and when I do this I get a rather cryptic runtime error. However, I have no errors when I use the following (though it doesn't satisfy my requirement):
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
Can someone help?
Try this
NSString *originalDateString = #"2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
NSDate *date = [dateFormatter dateFromString:originalDateString];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *newDateString = [dateFormatter stringFromDate:date];
Maybe if you use date formater to retrieve a NSDate instance and from there do you own formating.
NSSTring *dateString = "2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateFromWebApp = [dateFormatter dateFromString:dateString];
Haven't tested tho but it should work.
Cheers

Convert RSS date format to german date in Xcode

I have a little App which shows data from an RSS feed. Because it's date format is very long and now well for being printed out I want to convert it to the German's date format. This code I have now, but it always return "nil":
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormat setDateFormat:#"dd MMM, yyyy"];
NSString *contentDate = [dateFormat stringFromDate:date];
What's wrong with it? Thanks for answers!
RSS dates are a bit tricky.. you can grab the NSDate+InternetDateTime.h/.m from here:
https://github.com/mwaterfall/MWFeedParser
that should take care of the hard work.

How to convert a DD-MM-YYYY date format string into a YYYY-MM-DD date format string or into a NSDate object in Objective-C?

I have read date from XML which give me back string where the date is like DD-MM-YYYY. But when I want to add it to my core data database, SQLite sorts me that wrong so I have to convert it to date format or to string like: YYYY-MM-DD. But SQLite does not support either Date() or To_Date() functions. Can you help me?
You can use NSDateFormatter to format a date in Objective C.
Here's a quick example which might not do exactly what you want, but should hopefully give some pointers:
// Convert my dd-mm-yyyyy string into a date object
NSString *stringDate = #"01-02-2011";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:stringDate];
// Convert my date object into yyyy-mm-dd string object
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter stringFromDate:date];
[dateFormatter release];
Hope this helps!
Nick.
Using Nick's tutorial and answer the solution is :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSDate *date = [dateFormatter dateFromString:#"01.02.1989"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
// Convert my date object into yyyy-mm-dd string object
[dateFormatter1 setDateFormat:#"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter1 stringFromDate:date];
[dateFormatter release];
[dateFormatter1 release];
NSLog(formattedStringDate);
objektObrat.dateOfObrat = [[NSString alloc] initWithString:formattedStringDate];

NSDate formatter Problem its getting null while converting NSString to NSDate

hi to all
I am Struggling to convert NSString to NSDate with the Help of formatter but its going to be null
Can any one help please
here is my code...
NSString *dateString=[NSString stringWithFormat:#"10/26/2010/09:56:56 PM"];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"mm/dd/yyyy HH:mm:ss a"];
NSDate *localDateTime =[dateFormat dateFromString:dateString];
NSLog(#"client date is %#",localDateTime);
Thx in Advance
Could be because your dateString and format does not match.
10/26/2010/09:56:56 PM
mm/dd/yyyy HH:mm:ss a
Do you see the difference? :) They actually have to match. In the format you have a space between the year and hours and in the actual date there is a slash. Month you match by 'mm' which is actually minutes and should be 'MM'. Change to:
10/26/2010 09:56:56 PM
MM/dd/yyyy HH:mm:ss a
There might be more problems so I suggest you actually look at the guide to get it right.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
NSDate *formatterDate = [dateFormatter dateFromString:#"1999-07-11 at 10:30"];
[dateFormatter release];
Try this out.