为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

iOS
Smart猫小萌 2019-06-10 16:26:19
将NSString转换为NSDate(并再次返回)我将如何转换NSString就像“01/02/10“(指2010年2月1日)成为NSDate?我怎么能把NSDate回到一根绳子里?
查看完整描述

3 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

使出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"),                    Date.FromCalendar(2019, 2, 14))

XCTAssertEqual(Date.FromString("2019.02.14"),                    Date.FromCalendar(2019, 2, 14))

XCTAssertEqual(Date.FromString("2019/02/14"),                    Date.FromCalendar(2019, 2, 14))

XCTAssertEqual(Date.FromString("2019 Feb 14"),                   Date.FromCalendar(2019, 2, 14))

XCTAssertEqual(Date.FromString("2019 Feb 14th"),                 Date.FromCalendar(2019, 2, 14))

XCTAssertEqual(Date.FromString("20190214"),                      Date.FromCalendar(2019, 2, 14))

XCTAssertEqual(Date.FromString("02-14-2019"),                    Date.FromCalendar(2019, 2, 14))

XCTAssertEqual(Date.FromString("02.14.2019 5:00 PM"),            Date.FromCalendar(2019, 2, 14, 17))

XCTAssertEqual(Date.FromString("02/14/2019 17:00"),              Date.FromCalendar(2019, 2, 14, 17))

XCTAssertEqual(Date.FromString("14 February 2019 at 5 hour"),    Date.FromCalendar(2019, 2, 14, 17))

XCTAssertEqual(Date.FromString("02-14-2019 17:05:05"),           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"),       Date.FromCalendar(2019, 2, 14, 17, 05, 05))

XCTAssertEqual(Date.FromString("02-13-2019 Tomorrow"),           Date.FromCalendar(2019, 2, 14))

XCTAssertEqual(Date.FromString("2019 Feb 14th Tomorrow at 5"),   Date.FromCalendar(2019, 2, 14, 17))

就像:


extension Date

{



    public static func FromString(_ dateString: String) -> Date?

    {

        // Date detector.

        let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.date.rawValue)


        // Enumerate matches.

        var matchedDate: Date?

        var matchedTimeZone: TimeZone?

        detector.enumerateMatches(

            in: dateString,

            options: [],

            range: NSRange(location: 0, length: dateString.utf16.count),

            using:

            {

                (eachResult, _, _) in


                // Lookup matches.

                matchedDate = eachResult?.date

                matchedTimeZone = eachResult?.timeZone


                // Convert to GMT (!) if no timezone detected.

                if matchedTimeZone == nil, let detectedDate = matchedDate

                { matchedDate = Calendar.current.date(byAdding: .second, value: TimeZone.current.secondsFromGMT(), to: detectedDate)! }

        })


        // Result.

        return matchedDate

    }

}


使出NSString扩展为了这个。


// Simple as this.   

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

                           options:kNilOptions

                             range:NSMakeRange(0, [dateString length])

                        usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)

{ detectedDate = result.date; }];




查看完整回答
反对 回复 2019-06-10
  • 3 回答
  • 0 关注
  • 542 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信