nsdateformatter

Why can't I correctly parse this date string with NSDateFormatter?

社会主义新天地 提交于 2019-12-19 04:22:32
问题 I'm trying to parse a string that was generated by an NSDateFormatter using another NSDateFormatter with the same format. Rather than trying to make that previous sentence make sense, here's some code: NSDateFormatter *format = [[NSDateFormatter alloc] init]; [format setDateFormat:@"MMM dd, YYYY HH:mm"]; NSDate *now = [[NSDate alloc] init]; NSString *dateString = [format stringFromDate:now]; NSDateFormatter *inFormat = [[NSDateFormatter alloc] init]; [inFormat setDateFormat:@"MMM dd, YYYY HH

App Crashed when device time is in 24 hour format

[亡魂溺海] 提交于 2019-12-19 03:25:42
问题 In my App if IPhone device time is in 12 hour formate then date formatter works correctly but if device time is in 24 hour formate then app crashed. let dateFormatter = NSDateFormatter(); dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle; dateFormatter.dateFormat = "hh:mm a"; var dt1 = dateFormatter.dateFromString(arrSecond.objectAtIndex(n) as! String) 回答1: @Rajan Thanks for giving me idea of NSLocale . I set the dateformatter's

DateFormatter date from string returns nil

[亡魂溺海] 提交于 2019-12-19 00:19:36
问题 For some reason some devices fail to convert a string to a date. This is my date converting code: func dateFromString(string: String) -> Date? { let f = DateFormatter.init() f.dateFormat = "yyyy-MM-dd HH:mm:ss" return f.date(from: string) } Now, through my analytics platform I capture when this return nil. I have no clue why so. I assume it has something to do with local and maybe timezone or 12/24h settings but I can't figure out what is the problem. More Details... I have tracked the

DateFormatter date from string returns nil

心不动则不痛 提交于 2019-12-19 00:17:12
问题 For some reason some devices fail to convert a string to a date. This is my date converting code: func dateFromString(string: String) -> Date? { let f = DateFormatter.init() f.dateFormat = "yyyy-MM-dd HH:mm:ss" return f.date(from: string) } Now, through my analytics platform I capture when this return nil. I have no clue why so. I assume it has something to do with local and maybe timezone or 12/24h settings but I can't figure out what is the problem. More Details... I have tracked the

NSString to NSDate for yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 format

倾然丶 夕夏残阳落幕 提交于 2019-12-18 18:59:11
问题 My date string that I am getting is 2014-01-08T21:21:22.737+05:30 of this format. How do i confer it to NSDate? I tried: NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30"; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS+05:30"]; NSDate *currentDate = [dateFormatter dateFromString:currentDateString]; NSLog(@"CurrentDate:%@", currentDate); It returns nil. Any thoughts? 回答1: Use this code it is working fine your dateFormatter was wrong . NSDateFormatter *dateFormatter=[

Right way to convert string into NSDate?(iphone)

旧巷老猫 提交于 2019-12-18 18:24:44
问题 In my app I store the date as a string("MM/dd/YYYY" format). In the DB. later when I retrieve the string I have to compare two dates, how do I achieve this? If I had used the"YYYY/MM/dd" format I could have directly compared it as strings. Now I have to convert back into NSDate object using "nsdateformatter" and "dateFromString". But I get a constant date value using this no matter what I do. Any ideas?? 回答1: NSDateFormatter* df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MM/dd/yyyy

Convert String to NSDate with Swift 2

回眸只為那壹抹淺笑 提交于 2019-12-18 14:54:03
问题 I'm trying to convert a string to NSDate here is my code let strDate = "2015-11-01T00:00:00Z" // "2015-10-06T15:42:34Z" let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ssZ" print ( dateFormatter.dateFromString( strDate ) ) I keep getting nil as a result 回答1: The "T" in the format string needs to be single quoted so it will not be consider a symbol: Swift 3.0 let strDate = "2015-11-01T00:00:00Z" let dateFormatter = DateFormatter() dateFormatter.dateFormat =

Swift: can't get NSDate dateFormatter right

[亡魂溺海] 提交于 2019-12-18 09:46:42
问题 DateFormatter is acting funny to me: let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM'/'DD'/'YYYY" var dateString = "09/28/1989" var date = dateFormatter.dateFromString(dateString) println(date) Returns: "Optional(1988-12-24 13:00:00 +0000)" Any kind of help will be appreciated. 回答1: It looks like your date formatter is a bit off. "MM'/'DD'/'YYYY" reads "Padded month / day of the year (not month) / year of the current week (so Jan 1-6 could overlap and add side-effects)" I

Why is NSDateFormatter returning nil?

穿精又带淫゛_ 提交于 2019-12-18 09:13:26
问题 To be more precise this is not working for me: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"YYYY-MM-ddTHH:mm:ssZ"]; NSDate *prevday = [formatter dateFromString:@"2011-04-07T22:00:48Z"]; prevday is returning NIL. 回答1: You need to inserts ticks in the string: @"YYYY'-'MM'-'dd'T'HH':'mm':'ss'Z'" 回答2: According to this document; http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html%23/

NSDate date method returns wrong result

你说的曾经没有我的故事 提交于 2019-12-18 08:09:44
问题 I know there is a lot of questions of this type, but I did't find solution for my case; I need to get current and correct NSDate object, not NSString ! This code returns wrong time (+3 hours), because I'm from Ukraine. NSDate *currentDate = [NSDate date]; How to get current NSDate object from NSDateFormatter ? I know only how to get NSString , but I don't need it. EDIT: I'm using this code to compare 2 NSDates using NSCalendar object, here is code: NSCalendar *calendar = [NSCalendar