nsdateformatter

How can i convert the string to date and date from string?

拥有回忆 提交于 2019-12-06 10:47:27
My Input String is NSString *inputString=@"15 February 2012 17:05"; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"dd MMMM YYYY HH:mm"]; NSDate *dateVal = [dateFormat dateFromString:inputString]; NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init]; [dateFormat2 setDateFormat:@"MM/dd/YYYY HH:mm"]; NSString *outputString = [dateFormat2 stringFromDate:dateVal]; [dateFormat release]; [dateFormat2 release]; I got the following date outputString=12/25/2011 17:05 But it should be 02/15/2012 17:05..Where did i do the mistake? You are using wrong

iOS6 NSDateFormatter default year

守給你的承諾、 提交于 2019-12-06 09:07:43
问题 Has anyone noticed that iOS6 NSDateFormatter defaults to year 2000 when no year is given while in iOS6: [[NSDateFormatter alloc] init] dateFromString: @"7:30am"] => 2000 Jan 1st, 7:30am iOS5: => 1970 Jan 1st, 7:30am Question: 1. Is there a better way to compare two different times? I have this subway app PATH Schedule/Map. The subway times are hardcoded in the database as numSecondsSince1970. I give people the next train arriving by comparing departure times with the current

Formatting Date to dd-MMM in iOS

爱⌒轻易说出口 提交于 2019-12-06 08:12:13
I've been searching this and couldn't quite find it. I have an object in a NSDictionary that contains a NSDate. Now standard NSDate objects are pretty long. and I want to show the user in dd-MMM format. For eg: The original date may be 2012-04-23 00:00:00 +0000 , for which I want the date to appear as 23 Apr I tried using NSDateComponents and NSDateFormatter but it didn't quite work. Probably I'm not figuring out the correct usage FOR the required format. Here's the code for better understanding: NSLog(@"From Dictionary, Date = %@",[tmpDict objectForKey:@"eventDate"]); NSDateFormatter

How to convert string date into NSDate with NSDateFormatter

隐身守侯 提交于 2019-12-06 07:48:12
I have the following date as NSString: Thu May 29 14:22:40 UTC 2014 I've tried to convert it to NSDate with the following code: NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"EEE MMM d HH:mm:ss zzz yyyy"; NSDate *utc = [fmt dateFromString:@"Thu May 29 14:22:40 UTC 2014"]; NSLog(@"UTC Date:%@:", utc); The result is nil I've tried several dateFormat regex expressions but with no luck. What am I missing here? Use NSDataDetector class a subclasss of NSRegularExpression, its takes a string that of a unknown date and converts it to NSDate object if it finds a match.

Which is this NSDateFormatter style Friday, December 18th

梦想与她 提交于 2019-12-06 05:13:39
问题 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. 回答1: 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];

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

丶灬走出姿态 提交于 2019-12-06 03:05:29
问题 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 |

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

孤街浪徒 提交于 2019-12-06 02:22:19
问题 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

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

南楼画角 提交于 2019-12-06 02:21:41
问题 //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? 回答1: It can return nil if

NSDate formatter

流过昼夜 提交于 2019-12-06 01:59:09
I try to convert a string to NSDATE with no luck unfortunately. Friday, October 22, 11:26:45 ET 2010 I know the options for formatting (http://sree.cc/objective-c/nsdate-format-string-in-objective-c) but i cant get it to work. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"???????"]; anyone? Date Format Specifiers . So you'd probably need something like: [dateFormatter setDateFormat:@"eeee, MMMM dd, HH:mm:ss z yyyy"]; eeee - Local day of week spelled out MMMM - Month spelled out dd - day of month with no leading zeros HH - hour of day (24 hour

NSDateFormatter fails to return a datetime for UK region with 12 hour clock set [duplicate]

筅森魡賤 提交于 2019-12-06 01:37:47
This question already has answers here : What is the best way to deal with the NSDateFormatter locale “feechur”? (4 answers) Closed 3 years ago . This code works for every region/locale combination I can determine EXCEPT if I set my phone to UK region with the 12 hour clock set. Can anyone tell me why? This works for every region including the UK with 12 hour clock set: NSDateFormatter *theDateFormatter = [[NSDateFormatter alloc] init]; theDateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"; NSString *theLastFetchDateString = @"2015-11-13T01:47:52.4630000Z"; NSDate *theLastFetchDate =