nsdateformatter

Retrieve date and time from UIDatePicker iOS

耗尽温柔 提交于 2019-12-21 04:39:16
问题 I want to retrieve date and time in format e.g "Friday May 31, 2013 12:00 pm". How can I achieve that with NSDateFormatter? 回答1: NSDate *myDate = datePicker.date; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"]; NSString *prettyVersion = [dateFormat stringFromDate:myDate]; 来源: https://stackoverflow.com/questions/16559541/retrieve-date-and-time-from-uidatepicker-ios

NSDateformatter setDateFormat according to currentLocale

拟墨画扇 提交于 2019-12-21 03:55:23
问题 I'm going mad with, probably, a stupid problem. I have 3 strings: year, month and day. I need to have a date in the right format based on currentLocale, so i.e. if currentLocale localeIdentifier is en_US my dateFormat should be: MMM/dd/yyyy if it's fr_FR the dateFormat should be dd/MMM/yyyy I don't think the only way to do this is to get currentLocale localeIdentifier and start with a bunch of if then. Thanks in advance. Max 回答1: Look at NSDateComponents to create an NSDate, then use

NSDateFormatter in 12-hour mode

 ̄綄美尐妖づ 提交于 2019-12-21 03:13:48
问题 I have the following code. NSDateFormatter *df = ...; [df setTimeZone:[NSTimeZone defaultTimeZone]]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"]; NSDate * date = [df dateFromString:date_string]; //here is the problem In 24-hour mode everything is ok. When 12-hour mode is set on device, stringFromDate returns null. Format of date_string is the same all the time, date format too. Why does it happen? 回答1: Try to set the locale in this way : NSLocale *twelveHourLocale = [[NSLocale alloc]

NSDateFormatter in 12-hour mode

南笙酒味 提交于 2019-12-21 03:13:05
问题 I have the following code. NSDateFormatter *df = ...; [df setTimeZone:[NSTimeZone defaultTimeZone]]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"]; NSDate * date = [df dateFromString:date_string]; //here is the problem In 24-hour mode everything is ok. When 12-hour mode is set on device, stringFromDate returns null. Format of date_string is the same all the time, date format too. Why does it happen? 回答1: Try to set the locale in this way : NSLocale *twelveHourLocale = [[NSLocale alloc]

How to turn a NSString into NSDate?

不想你离开。 提交于 2019-12-21 02:54:06
问题 Ive been racking my brains with no luck. Could someone please tell me how i would convert this string: "2011-01-13T17:00:00+11:00" into a NSDate? 回答1: The unicode date format doc is here Also, for your situation, you could try this: // original string NSString *str = [NSString stringWithFormat:@"2011-01-13T17:00:00+11:00"]; // convert to date NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; // ignore +11 and use timezone name instead of seconds from gmt [dateFormat setDateFormat:

NSFetchedResultsController titleForHeaderInSection with formatted NSDate

白昼怎懂夜的黑 提交于 2019-12-21 02:47:40
问题 In my Core Data app I am using a FetchedResultsController. Usually to set titles for headers in a UITableView you would implement the following method like so: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section]; return [sectionInfo name]; } where [sectionInfo name] returns a NSString. my sectionKeyPath is based on an NSDate and this all

Parsing rfc3339 dates with NSDateFormatter in iOS 4.x and MacOS X 10.6: impossible?

血红的双手。 提交于 2019-12-20 11:48:26
问题 Parsing a rfc3339 date with NSDateFormatter appears to be impossible, in the general case. Am I wrong? [Edit 2 years later: there is now a way! See below and footnote.] A not-especially-malleable web service is feeding me dates like: 2009-12-31T00:00:00-06:00 Rfc3339 compliant, default output of the jaxb library they're using. Note the colon, which rfc3339 requires when the offset isn't a literal "z": time-numoffset = ("+" / "-") time-hour ":" time-minute time-offset = "Z" / time-numoffset I

Date/Time Natural Language Approximation in Swift

╄→гoц情女王★ 提交于 2019-12-20 09:04:12
问题 I am attempting to convert a UTC formatted date from an API to a human-readable approximation format using Swift. I'm looking for something like the following: 2015-07-14T13:51:05.423Z to About two weeks ago What is the best approach to this in Swift? While optimally this could format strings directly, I understand that this will probably require casting the string to a NSDate object. Any help would be greatly appreciated. EDIT: My question had been identified as a possible duplicate of

Age extracted from birth date always off by inconsistent amount

强颜欢笑 提交于 2019-12-20 07:43:50
问题 I'm using the following code to convert a user-supplied birthdate to its equivalent years from the current date. The output is always off by an inconsistent amount in years and very large numbers in days and months. NSDateFormatter *tempFormatter = [[NSDateFormatter alloc] init]; [tempFormatter setDateFormat:@"yyyy-mm-dd hh:mm:ss"]; NSDate *birthDate = [tempFormatter dateFromString:[NSString stringWithFormat:@"%@-%@-%@ 01:00:00",self.birthYear.text,self.birthMonth.text,self.birthDay.text]];

NSDateFormatter for datetime [duplicate]

孤者浪人 提交于 2019-12-20 07:41:12
问题 This question already has answers here : How do I get an ISO 8601 date on iOS? (10 answers) Closed 4 years ago . I have a string which is coming from an API that I need to convert into a NSDate but I'm uncertain which dateFormat to use for NSDateFormatter . let openedAt = "2015-06-30T12:34:00.000-04:00" // coming from API let formatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd..." // not sure what format to use here if let date = formatter.dateFromString(openedAt) { println(date)