nsdateformatter

NSDateFormatter returns null for specific dates

为君一笑 提交于 2019-12-04 05:23:34
I'm having a problem with the NSDateFormatter . It is returning null for some specific dates. I did this function to debug the case: - (void) debugTest:(NSString *)dateStr{ NSLog(@"String: %@", dateStr); NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd"]; NSDate *dateObj = [df dateFromString: dateStr]; NSLog(@"Date: %@", dateObj); } This is my log output for some dates: String: 2012-10-18 Date: 2012-10-18 03:00:00 +0000 String: 2012-10-19 Date: 2012-10-19 03:00:00 +0000 String: 2012-10-20 Date: 2012-10-20 03:00:00 +0000 String: 2012-10-21 Date: (null) String

Date formatter returns nil for June

非 Y 不嫁゛ 提交于 2019-12-04 02:53:32
I have a strange problem with those lines of code : NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MMyyyy"]; NSDate * date = [df dateFromString:@"062008"]; NSLog(@"Date %@", date); The result is : Date (null) But when I change the month like this : NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MMyyyy"]; NSDate * date = [df dateFromString:@"072008"]; NSLog(@"Date %@", date); The result is : Date 2008-06-30 23:00:00 +0000 For only the month of June (06), the date conversion from string fails ! I think, it's something related to timezone,

Shared NSDateFormatter - Best Practices?

久未见 提交于 2019-12-04 02:35:39
My team found that we were using a variety of NSDateFormatter objects throughout our code base, and started looking into how we could avoid the cost/confusion of allocating/initializing common formatters in a bunch of separate places. One idea we had was to create a category on the NSDateFormatter class that would provide a reference to a static instance of a commonly configured formatter. For example, we were using the "short time" date formatter in several places, and were looking to add the following class method: @implementation NSDateFormatter (NSDateFormatter_PDDateFormatters) static

NSDateFormatter for BST instead of GMT+1

╄→尐↘猪︶ㄣ 提交于 2019-12-04 02:29:55
问题 I want to display the following string on my time axis: "GMT/BST" Here's the code: NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"zzz"]; timeZoneString = [NSMutableString stringWithFormat:@"%@ / %@",[dateformatter stringFromDate:startDate],[dateformatter stringFromDate:endDate]]; But this gives "GMT/GMT+01:00" What is the NSDateFormatter code to turn "GMT+01:00" into "BST" ? I can't get the right formatters to do this, having tried z|zzz|Z|ZZZ|v

NSDateFormatter will not parse string with timezone that includes colon

故事扮演 提交于 2019-12-04 02:27:55
问题 I'm trying to transform @"Fri, 26 Aug 2011 10:51:00 +02:00" into an NSDate : [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"]; NSDate *date = [dateFormatter dateFromString:dateString]; I get nil as a result; what am I doing wrong? 回答1: The colon in the timezone (+02:00) is the issue. According to the Unicode Standard #35, 1..3 capital Z pattern denotes a RFC 822 time zone. RFC 822 time zones represent the offset from GMT (or UTC) and have the following format: zone = "UT" / "GMT"

NSDateFormatter relative date formatting with custom format

元气小坏坏 提交于 2019-12-04 02:22:29
So my intention is to put out dates that would look like the following: Today, August 28 Tomorrow, August 29 Friday, August 30 ...etc The issue is that it seems I can only get so close. When I setDoesRelativeDateFormatting:YES and setDateStyle to Full and setTimeStyle to None , it yields results like this: Today Tomorrow Friday, August 30, 2013 This yields the year and does not yield month and date for today and tomorrow. The other code I've tried is this: [NSDateFormatter dateFormatFromTemplate:@"eeeedMMM" options:0 locale:[NSLocale currentLocale]]; and that yields the following: Wednesday,

Converting NSString to NSDate - wrong format

天大地大妈咪最大 提交于 2019-12-04 02:13:07
问题 Hi I am trying to convert a string into NSDate format - but it returns value with wrong format and also in GMT time. NSString *myString = @"10:30 AM"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSTimeZone *local = [NSTimeZone systemTimeZone]; dateFormatter.dateFormat = @"hh:mm a"; [dateFormatter setTimeZone:local]; NSDate *myDate = [dateFormatter dateFromString:myString]; NSLog(@"myDate---%@",myDate); But this returns myDate---2000-01-01 05:30:00 +0000 Where I am making

convert string to NSDate with custom format [duplicate]

点点圈 提交于 2019-12-04 02:09:03
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: NSString to NSDate iPhone: How to convert “yyyyMMddThhmmss” format string to NSDate? How do I convert this format to NSDate ? My string date format is: yyyy-m-d'T'H:m:ss (no space between intervals). Update: 2012-11-06T12:17:22.947 would be the string that I'd like to convert to NSDate 回答1: just paste this method in your .m file and then call this method with your stringDate.. -(NSDate *)convertStringToDate:

NSDateFormatter Locale

二次信任 提交于 2019-12-03 20:54:28
问题 How to set Locale for NSDateFormatter? I've tried the below code and its not working. - (NSString *)localizedStringWithFormat:(NSString *)format { NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:format]; NSCalendar *cal = [NSCalendar currentCalendar]; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier: @"fr_FR"]; cal.locale = locale; df.calendar = cal; return [df stringFromDate:self]; } Please let me know how to make this work. Thanks. 回答1: NSDateFormatter

Converting date of format yyyy-MM-dd'T'HH:mm:ss.SSS

China☆狼群 提交于 2019-12-03 20:27:56
问题 I have few NSDate objects which contain values compliant to this format yyy-MM-dd'T'HH:mm:ss.SSS When I try to convert to a different format such as MMM dd, yyyy HH:mm the formatter always returns nil . However, if I hardcode the string value I get through the NSDate object , it is transformed to the new format without a problem. I populate my NSDate object in the model using the setValue:forKey: method, and I feel the problem is here where the JSON library passes a string value. However, the