I'm using the following code to convert NSString to NSDate:
NSDate *date = [NSDate convertStringToDate:strDate andFormatter:#"MM dd"];
+ (NSDate *)convertStringToDate:(NSString *)dateString andFormatter:(NSString *)formatter
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:formatter];
dateFormatter.timeZone = [NSTimeZone systemTimeZone];
return [dateFormatter dateFromString:dateString];
}
NSString is #"07 15". But my NSDate is nil. On a simulator it is fine but on device with iOS7 it is nil.
Do you have any suggestions about it?
If your code works in iOS 6, then this may just be a bug in iOS 7. Be sure to file a bug report if you think that's the case.
If it doesn't work in iOS 6, then it may be that a month and day aren't sufficient to specify a date. Try adding a year and see if you get a valid date that way.
Note that questions about iOS 7 should be posted to Apple's developer forums, not here.
you may need to set the locale like this:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
I solved this problem including the locale to the formatter.
hope this helps.
Related
I want to ask that how i get the device date and time format in my like device setting am/pm or 24 hrs what time and what format is set???
Like if i use this format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"mm-dd-yyyy"];
insertCmd = [insertCmd stringByAppendingString:formatter setDateFormat:#"MM.dd.yyyy"];
it set the date format but i want the setting user set in device?
Try this,
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(#"date = %#",[dateFormatter stringFromDate:date]);
This is for converting current date to string. It should be based on the currentLocale of the device. You dont have to set anything. However I am not sure what you meant by the code in your question.
This is what mentioned in documentation,
The format for these date and time styles is not exact because they
depend on the locale, user preference settings, and the operating
system version. Do not use these constants if you want an exact
format, for example if you are parsing an external data file which
contains date information in a fixed format. There are several
different “lengths” of the formats:
Or:
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setFormat:#"mm-dd-yyyy"];
NSString* dateString = [formatter stringFromDate:date];
insertCmd = [insertCmd stringByAppendingString:dateString];
I am beginner in ios development.
I am stuck in NSdateFormatter class.
My question is:
Which kind of formatter support on this date "Oct, 25th"?
Please help me,
Thanks in advance...
you can format date by "Oct, 25" you must append "th" with the formatted date
I would like te stress you not to use explicit date formats, since if the users setting is not set to english the date might not be presented correctly.
Instead you should use the dateStyle and timeStyle properties of NSDateFormatter.
See Apple NSDateFormatterStyle documentation to see which one will work for you.
[yourDateFormatter setDateFormat:#"LLL,dd'th'"];
LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
dd: 01~31 ( Day of Month)
for more info about date-formate see my blog from this link..
MyBlog
For Example
call this method with bellow 2 line code..
NSString *strDate = [self StringFromDate:#"Oct, 25th"];
NSLog(#"\n New Date is HERE =====>> %#",strDate);
this is the method just paste in your .m file
-(NSString *)StringFromDate:(NSString *)DateLocal{
// DateLocal = [self trimString:DateLocal];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"LLL,dd'th'"];
NSDate *date = [dateFormatter dateFromString: DateLocal];
NSString *tt = [dateFormatter stringFromDate:date];
NSDate *dateReturn = [dateFormatter dateFromString:tt];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd,LLL"];// set the format which you want
NSString *dateString = [dateFormat stringFromDate:dateReturn];
NSLog(#"Date is HERE =====>> %#",dateString);
[dateFormat release];
return dateString;
}
I just referred to the Q & A at iOS reference for NSDateFormatter.
Link: http://developer.apple.com/library/ios/#qa/qa2010/qa1480.html
Here is my problem I get a string from a webservice which is of this format:
"dd-mm-yyyy xx:xx:xx AM"
My actual purpose is just to use the date and not the time at all. But I just use the same format in the NSDateFormatter and I was able to get the answers properly for all date related problems except on one mobile which is iOS 4.1.
Please let me know what is the most optimal solution for this problem. I think, I should just use date and that will solve my problem or any other suggestions for this problem?
Your question wasn't really helpful in terms of the result you get form iOS 4.1. Anyways...
Try this...
// convert date
NSString *webStr = [NSString stringWithFormat:#"28-10-2010 04:44:22 AM"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm:ss a"];
NSDate* date = [formatter dateFromString:webStr];
// set up the new date format
[formatter setDateFormat:#"dd-MM-yyyy"];
NSString *myDate = [formatter stringFromDate:date];
[formatter release];
I'm trying to use the NSDate dateFromString method but I'm getting an warning and it's crashing the app. The code looks like:
NSString *pickerDate = [NSString stringWithFormat:#"%#", timeSelector.date];
NSDate *defaultDate = [NSDate dateFromString:pickerDate];
The warning is:
'NSDate' may not respond to '+dateFromString'.
It appears that method is deprecated (in the midst of an upgrade from XCode 2 to 3.
What alternate method can I use to create a date from a string?
NSDateFormatter is the intended way for you to get an NSDate from an NSString.
The most basic usage is something like this:
NSString *dateString = #"3-Aug-10";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"d-MMM-yy";
NSDate *date = [dateFormatter dateFromString:dateString];
I had a same problem.
I changed the dateFormat from #"YYYY/M/d H:m:s" to #"YYYY/MM/dd HH:mm:ss" as follows.
Then it works on my iPhone 4. (Xcode 4.5 for iOS 6.)
NSDateFormatter *dateFormatter1;
NSString *dateStr1=#"";
NSDate *gantanDate1;
dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setLocale:[NSLocale systemLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
dateFormatter1.dateFormat=#"YYYY/MM/dd HH:mm:ss";
dateStr1=#"2012/1/1 00:00:00"];
// in his case, dateStr1=pickerDate;
gantanDate1 = [dateFormatter1 dateFromString:dateStr1];
Hi Silber everyone says the same thing to convert the string date to date object.Try this i think you have used two date formatters in two places where you are saving date to string and getting date from string.right try to use the same date formatters in both places. It will solve your problem.
I'm having a problem. I get incoming time strings in 12-hour format, and I'm turning them into NSDate objects. When the iPhone is in 12 hour format, no problem. But when it's in 24 Hour format, things go wrong. Here's some sample code to demonstrate:
NSString *theTime = #"3:19 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"]; // "3:19 PM"
NSDate *date = [formatter dateFromString:theTime];
NSString *theString = [formatter stringFromDate:date];
In 24 hour mode, date is 1970-01-01 03:19:00, and theString is "3:19" - WRONG
In 12 hour mode, date is 1970-01-01 15:19:00, and theString is "3:19 PM" - RIGHT
So... question 1: why is the device's 24 hour setting overriding my date formatter setting?
and more importantly, question 2: How do I get a proper conversion from 12 hour time to 24 hour time?
I already have code to detect if the phone is in 24 hour mode, but other than digging around in the string and swapping the 3 with a 15, there doesn't seem to be a clean way to do this.
Not sure if you still need it, but I've had a similar problem which got solved by setting the locale for the date formatter. That is, if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX.
The reason for this behaviour is Locale, set the correct Locale
NSString *strAgendaDate = #"01/17/2012 12:00 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[dateFormatter setDateFormat:AgendaDateFormatForMeeting];
NSDate *meetingDate = [dateFormatter dateFromString:aStrDate];
[dateFormatter setDateFormat:AgendaDateRepresentation];
strAgendaDate = [dateFormatter stringFromDate:meetingDate];
It works for both 24-hour and 12 hour format
I believe the #"h:mm a" should be #"HH:mm a".
If you use the pre-build dateformatter in cocoa, everything will be taken care of for you.
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
NSDateFormatterShortStyle and NSDateFormatterNoStyle comes in different varieties.
Using those will make sure you respect the settings the user has selected for dates and times.
The 12-14 hour clock conversion is taken care of by the SDK, if you have a model or some value object for storing your dates try to keep them as NSDate. This way you can format them only when you need to display them. Saving dates as strings could open a world of trouble when you maybe parse them from xml where the GMT is specified separately or try to add and subtract NSTimeIntervals.
I changed from #"hh:mm:ss" to #"HH:mm:ss" and time style was changed from "1:03 PM" to "13:03".
Hope this will help you.
Okay, I left a comment, but it squished all the code together, so I'll have to "answer" my question with a comment:
Thanks. I gave it a whirl with this code:
NSString *theTime = #"3:19 PM";
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
NSDate *date = [timeFormatter dateFromString:theTime];
NSString *theString = [timeFormatter stringFromDate:date];
And date comes up nil. I ran into this earlier when I tried this route, and it's not working. Very frustrating.