将NSString转换为NSDate(并再次返回)

将NSString转换为NSDate(并再次返回)

我将如何转换NSString就像“01/02/10“(指2010年2月1日)成为NSDate?我怎么能把NSDate回到一根绳子里?



Smart猫小萌
浏览 543回答 3
3回答

互换的青春

使出Date延拓为了这个。它用NSDataDetector而不是NSDateFormatter.// Just throw at it without any format.var date: Date? = Date.FromString("02-14-2019 17:05:05")令人愉快的是,它甚至能识别出这样的东西“明天5点”.XCTAssertEqual(Date.FromString("2019-02-14"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date.FromCalendar(2019, 2, 14))XCTAssertEqual(Date.FromString("2019.02.14"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date.FromCalendar(2019, 2, 14))XCTAssertEqual(Date.FromString("2019/02/14"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date.FromCalendar(2019, 2, 14))XCTAssertEqual(Date.FromString("2019 Feb 14"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date.FromCalendar(2019, 2, 14))XCTAssertEqual(Date.FromString("2019 Feb 14th"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date.FromCalendar(2019, 2, 14))XCTAssertEqual(Date.FromString("20190214"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date.FromCalendar(2019, 2, 14))XCTAssertEqual(Date.FromString("02-14-2019"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date.FromCalendar(2019, 2, 14))XCTAssertEqual(Date.FromString("02.14.2019 5:00 PM"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date.FromCalendar(2019, 2, 14, 17))XCTAssertEqual(Date.FromString("02/14/2019 17:00"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date.FromCalendar(2019, 2, 14, 17))XCTAssertEqual(Date.FromString("14 February 2019 at 5 hour"),&nbsp; &nbsp; Date.FromCalendar(2019, 2, 14, 17))XCTAssertEqual(Date.FromString("02-14-2019 17:05:05"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date.FromCalendar(2019, 2, 14, 17, 05, 05))XCTAssertEqual(Date.FromString("17:05, 14 February 2019 (UTC)"), Date.FromCalendar(2019, 2, 14, 17, 05))XCTAssertEqual(Date.FromString("02-14-2019 17:05:05 GMT"),&nbsp; &nbsp; &nbsp; &nbsp;Date.FromCalendar(2019, 2, 14, 17, 05, 05))XCTAssertEqual(Date.FromString("02-13-2019 Tomorrow"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date.FromCalendar(2019, 2, 14))XCTAssertEqual(Date.FromString("2019 Feb 14th Tomorrow at 5"),&nbsp; &nbsp;Date.FromCalendar(2019, 2, 14, 17))就像:extension Date{&nbsp; &nbsp; public static func FromString(_ dateString: String) -> Date?&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Date detector.&nbsp; &nbsp; &nbsp; &nbsp; let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.date.rawValue)&nbsp; &nbsp; &nbsp; &nbsp; // Enumerate matches.&nbsp; &nbsp; &nbsp; &nbsp; var matchedDate: Date?&nbsp; &nbsp; &nbsp; &nbsp; var matchedTimeZone: TimeZone?&nbsp; &nbsp; &nbsp; &nbsp; detector.enumerateMatches(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in: dateString,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options: [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; range: NSRange(location: 0, length: dateString.utf16.count),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (eachResult, _, _) in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Lookup matches.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matchedDate = eachResult?.date&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matchedTimeZone = eachResult?.timeZone&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Convert to GMT (!) if no timezone detected.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if matchedTimeZone == nil, let detectedDate = matchedDate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { matchedDate = Calendar.current.date(byAdding: .second, value: TimeZone.current.secondsFromGMT(), to: detectedDate)! }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; // Result.&nbsp; &nbsp; &nbsp; &nbsp; return matchedDate&nbsp; &nbsp; }}使出NSString扩展为了这个。// Simple as this.&nbsp; &nbsp;date = dateString.dateValue;感谢NSDataDetector,它能识别出大量的格式。'2014-01-16' dateValue is <2014-01-16 11:00:00 +0000>'2014.01.16' dateValue is <2014-01-16 11:00:00 +0000>'2014/01/16' dateValue is <2014-01-16 11:00:00 +0000>'2014 Jan 16' dateValue is <2014-01-16 11:00:00 +0000>'2014 Jan 16th' dateValue is <2014-01-16 11:00:00 +0000>'20140116' dateValue is <2014-01-16 11:00:00 +0000>'01-16-2014' dateValue is <2014-01-16 11:00:00 +0000>'01.16.2014' dateValue is <2014-01-16 11:00:00 +0000>'01/16/2014' dateValue is <2014-01-16 11:00:00 +0000>'16 January 2014' dateValue is <2014-01-16 11:00:00 +0000>'01-16-2014 17:05:05' dateValue is <2014-01-16 16:05:05 +0000>'01-16-2014 T 17:05:05 UTC' dateValue is <2014-01-16 17:05:05 +0000>'17:05, 1 January 2014 (UTC)' dateValue is <2014-01-01 16:05:00 +0000>部分艾普斯!基特,抓住这个类别NSString+EPPZKit.h来自GitHub。无论您不确定(或不关心)字符串中包含的日期格式,使用NSDataDetector解析日期.//Role players.NSString *dateString = @"Wed, 03 Jul 2013 02:16:02 -0700";__block NSDate *detectedDate;//Detect.NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingAllTypes error:nil];[detector enumerateMatchesInString:dateString&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options:kNilOptions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;range:NSMakeRange(0, [dateString length])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop){ detectedDate = result.date; }];
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS