nsdateformatter

issue with date formatting using NSDateFormatter

半腔热情 提交于 2019-12-04 15:11:30
问题 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

Show time like a digital clock

流过昼夜 提交于 2019-12-04 14:59:47
I am able to display the current time on my iPad application using the code, NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeStyle: NSDateFormatterShortStyle]; NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]]; timeLabel.text = currentTime; But this gives the time only when the application is loaded. How do i get the time to keep running? Like a digital clock. Use this: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeStyle: NSDateFormatterShortStyle]; [NSTimer scheduledTimerWithTimeInterval:1.0

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

ぃ、小莉子 提交于 2019-12-04 14:33:38
问题 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

Which is this NSDateFormatter style Friday, December 18th

雨燕双飞 提交于 2019-12-04 11:16:54
I want to know which is this date format Friday, December 18th and also is this standard date format or i have to some hard work to get this. Thanks. I don't think the th is possible to achieve via formatters. Although you can do it like this: NSDate *today = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"EEEE, MMMM d"]; NSString *dateString = [dateFormat stringFromDate:today]; NSCalendar *cal = [NSCalendar currentCalendar]; unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents *dtComp =

iPhone SDK Objective-C __DATE__ (compile date) can't be converted to an NSDate

此生再无相见时 提交于 2019-12-04 08:07:31
//NSString *compileDate = [NSString stringWithFormat:@"%s", __DATE__]; NSString *compileDate = [NSString stringWithUTF8String:__DATE__]; NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; [df setDateFormat:@"MMM d yyyy"]; //[df setDateFormat:@"MMM dd yyyy"]; NSDate *aDate = [df dateFromString:compileDate]; Ok, I give up. Why would aDate sometimes return as nil? Should it matter if I use the commented-out lines... or their matching replacement lines? It can return nil if the phone's Region setting is not US (or equivalent). Try setting the formatter's locale to en_US: NSString

How to convert date (without knowing the kind) to string

无人久伴 提交于 2019-12-04 07:44:05
I have an array of NSDate objects that I'm trying to convert to a string . My way of converting them is with NSDateFormat , but, if there's another better way, I'd be happy to hear. The most things a date would have is: Days-Hours-Minutes-Seconds-Milliseconds But some will have less, like: Minutes-Seconds-Milliseconds Or even less. Now I will have to convert them to a string . Would I have to create if statements to determine if it has days, hours, and so on, or is there an easier way of doing this? Example using NSDataDetector (Swift 2.0): let textDate = "dan's march 31, 1975 12:34 pm"; do {

What kind of sarcastic error is this iOS?

天大地大妈咪最大 提交于 2019-12-04 07:33:58
问题 I have some code I use to sort calendar dates that looks like this: #if !(TARGET_IPHONE_SIMULATOR) NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0 locale:[NSLocale currentLocale]]; [fmt setDateFormat:formatString]; #else [fmt setDateFormat:@"HH:mm dd MMM yyyy"]; #endif If I run it in the simulator all is ok. If I run it on the device I get this sarcastic debug message. 2012-09-19 22:40:13.972 APPNAME [4923:907] * -[__NSCFCalendar components

NSString to NSDate conversion getting the wrong result [duplicate]

偶尔善良 提交于 2019-12-04 07:33:07
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Wrong time from NSDateFormatter NSDate is 5 hours off I am trying to convert NSString to NSDate with the code result = @"2012-02-09"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:result]; [dateFormatter release]; NSLog(@"date: %@", dateFromString); But

Can you override NSDateFormatter 12 vs 24 hour time format without using a custom dateFormat

允我心安 提交于 2019-12-04 06:38:12
问题 I want to convert a Date as a display string in the user's time zone, but I want to use 24 hour time format rather than 12 hour time format. I know I can do that by explicitly setting a custom dateFormat string, but that loses the benefit of letting the system generate strings that conform to the user's locale setting. NSNumberFormatter has settings that let you change aspects of the conversion, like the decimal separator. Is there a setting for NSDateFormatter that will let me switch between

NSDateComponentsFormatter's stringFromDate(_, toDate:) returns nil

天涯浪子 提交于 2019-12-04 06:12:15
Question Why is string nil? let formatter = NSDateComponentsFormatter() let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0) let intervalDate = NSDate(timeInterval: 3628810, sinceDate: referenceDate) let string = formatter.stringFromDate(referenceDate, toDate: intervalDate) I'm expecting a string like "6w 10s" to be returned. (6 weeks is 3,628,800 seconds.) Attempted Troubleshooting To troubleshoot, I tried setting allowedUnits : formatter.allowedUnits = .YearCalendarUnit | .MonthCalendarUnit | .WeekCalendarUnit | .DayCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit |