nsdateformatter

NSDateFormatter in 12-hour mode

我的梦境 提交于 2019-12-03 09:35:31
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? Try to set the locale in this way : NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; df.locale = twelveHourLocale; To force instead to 24 hour, you can

NSDateFormatter returns nil for @“dd-MM-yy” in iOS 3.0

醉酒当歌 提交于 2019-12-03 09:01:51
I have this part of code: NSDate *date =nil; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setDateFormat:@"dd-MM-yy"]; date = [dateFormatter dateFromString:inString]; [dateFormatter release]; It works perfectly fine, as expected in iOS 4.0. But the same code doesnt in 3.0. The string which I am getting, is like "12-Nov-10" and this is contained in inString pointer. The date formatter returns nil if the native OS is 3.0 or 3.1. For some reasons I need to stick to the same date format. Has anyone else

issue with date formatting using NSDateFormatter

痴心易碎 提交于 2019-12-03 08:45:26
I have a date string which I want to convert to another format. The original date string example is : "2013-06-04 02:19:21 +0000" I want to convert this to "Wed, Jun 4" NSString * date_string = @"2013-06-04 02:19:21 +0000"; NSDateFormatter *complexdateFormater = [[NSDateFormatter alloc] init]; [complexdateFormater setDateFormat:@"yyyy-M-DD HH:mm:ss Z"]; NSDate* complexdate = [complexdateFormater dateFromString:date_string]; NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"EE, MMM d" options:0 locale:[NSLocale currentLocale]]; NSDateFormatter *dateFormatter = [[NSDateFormatter

NSFetchedResultsController titleForHeaderInSection with formatted NSDate

↘锁芯ラ 提交于 2019-12-03 08:21:26
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 works fine apart from the section headers it gives me are the raw date description strings (e.g. 12/12

How to turn a NSString into NSDate?

时光怂恿深爱的人放手 提交于 2019-12-03 08:21:20
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? 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:@"YYYY-MM-dd'T'HH:mm:ss'+11:00'"]; [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Australia

NSDateFormatter “Month” in 3 letters instead of full word

你说的曾经没有我的故事 提交于 2019-12-03 05:36:29
问题 NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd-MM-YYYY HH:mm"]; [formatter setTimeZone:[NSTimeZone systemTimeZone]]; If i choose MM i get the month in number: 09-05-2012 15:33 If i choose MMMM i get the month in word: 09-May-2012 15:33 What i wanted, was the month in 3 letters abbreviation. Eg: January would be Jan In this case May is correct because it only has 3 letters. 回答1: Have you tried: [formatter setDateFormat:@"dd-MMM-YYYY HH:mm"]; and then

How to display time in 12 hour format in Objective-C?

核能气质少年 提交于 2019-12-03 05:17:36
问题 I was working on an iPhone application, and I need to display the time in a label. So I wrote the follow: NSDate *date = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm a"]; NSString *formattedDateString = [dateFormatter stringFromDate:date]; timeLabel.text = formattedDateString;** It displays time in label but in 24H format. Example: 15.00 PM instead of 3.00 PM I want to display time in 12h format, i.e. 3.00 PM What should

Convert NSDate to NSString with NSDateFormatter with TimeZone without GMT Time Modifier

风流意气都作罢 提交于 2019-12-03 04:46:31
I'm initializing my NSDateFormatter thusly: NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSDate *date = [NSDate date]; NSString *dateString = [dateFormatter stringFromDate:date]; dateString is now: Thu, 29 Jul 2010 14:58:42 GMT+00:00 I want to get rid of the "+00:00" I'm guessing from http://unicode.org/reports/tr35/tr35-6.html#Time

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

烂漫一生 提交于 2019-12-03 02:55:27
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 want to parse these into NSDates. NSDateFormatter wants patterns in the syntax specified by Unicode ,

Inconsistent behaviour with NSDateFormatter on two different devices

微笑、不失礼 提交于 2019-12-03 02:34:43
I'm having a bit of a problem with NSDateFormatter failing on one user's device (returning nil when parsing a string) and working perfectly when I run it locally (either in the simulator or on my device). I'm trying to rule out what could be causing a difference in this behaviour. My first thought was the locale but I've tried setting it explicitly to ensure the same locale is always used but it makes no difference. Here is the code: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; NSLocale *locale = [[NSLocale alloc]