Swift 5:如何根据提供的字符串计算正确的Date? [英] Swift 5: How to calculate correct Date from provided string?

查看:90
本文介绍了Swift 5:如何根据提供的字符串计算正确的Date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不擅长Swift,所以我想知道如何计算当前时间在两次之间.

I am not good at Swift so I would like to know how to calculate the current time is between two times.

我从后端得到以下回应. {"workHours":"MF 9:00-18:00"}

I got the following response from the backend. {"workHours":"M-F 9:00 - 18:00"}

如何从"MF 11:00-20:00"获取开始(9:00)时间,结束(18:00)时间使用正则表达式和DateFormatter进行响应?(我不确定这是从这种字符串中获取Date对象的正确方法,如果不正确,请推荐最佳方法和经验)

How to get start(9:00) time, end(18:00) time from "M-F 11:00 - 20:00" response using regular expression and DateFormatter?(I am not sure it is the correct way to get Date object from this kind of string, if not please recommend the best way and experience)

推荐答案

您可以使用以下正则表达式:

You can use the following regex:

"^\\w-\\w\\s{1}(\\d{1,2}:\\d{2})\\s{1}-\\s{1}(\\d{1,2}:\\d{2})$"

崩溃

^声明行首的位置
A-Z一个字符,介于A(索引65)和Z(索引90)(区分大小写)之间
-匹配字符-按字面意义(区分大小写)
A-Z是介于A(索引65)和Z(索引90)(区分大小写)之间的单个字符
\ s匹配任何空白字符(等于[\ r \ n \ t \ f \ v]){1}量词—精确匹配一次无意义的量词)
第一捕获组(\ d {1,2}:\ d {2})
\ d {1,2}匹配一个数字(等于[0-9])
{1,2}量词-匹配1到2次,尽可能多地匹配,并根据需要返回(贪婪)
:匹配字符:按字面值(区分大小写)
\ d {2}匹配一个数字(等于[0-9])
\ s匹配任何空白字符(等于[\ r \ n \ t \ f \ v]){1}量词-精确匹配一个时间(无意义的量词)
-匹配字符-按字面值(区分大小写)
\ s {1}匹配任何空白字符(等于[\ r \ n \ t \ f \ v]){1}量词-精确匹配一次(无意义的量词)
第二捕获组(\ d {1,2}:\ d {2})
$声明行尾的位置

^ asserts position at start of a line
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
- matches the character - literally (case sensitive)
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
\s matches any whitespace character (equal to [\r\n\t\f\v]) {1} Quantifier — Matches exactly one time meaningless quantifier)
1st Capturing Group (\d{1,2}:\d{2})
\d{1,2} matches a digit (equal to [0-9])
{1,2} Quantifier — Matches between 1 and 2 times, as many times as possible, giving back as needed (greedy)
: matches the character : literally (case sensitive)
\d{2} matches a digit (equal to [0-9])
\s matches any whitespace character (equal to [\r\n\t\f\v]) {1} Quantifier — Matches exactly one time (meaningless quantifier)
- matches the character - literally (case sensitive)
\s{1} matches any whitespace character (equal to [\r\n\t\f\v ]) {1} Quantifier — Matches exactly one time (meaningless quantifier)
2nd Capturing Group (\d{1,2}:\d{2})
$ asserts position at the end of a line


let response = "M-F 11:00 - 20:00"
let pattern = "^[A-Z]-[A-Z]\\s{1}(\\d{1,2}:\\d{2})\\s{1}-\\s{1}(\\d{1,2}:\\d{2})$"
let regex = try! NSRegularExpression(pattern: pattern)
if let match = regex.matches(in: response, range: .init(response.startIndex..., in: response)).first,
    match.numberOfRanges == 3 {
    match.numberOfRanges
    let start = match.range(at: 1)
    print(response[Range(start, in: response)!])
    let end = match.range(at: 2)
    print(response[Range(end, in: response)!])
}

这将打印

11:00
20:00

11:00
20:00

要获取开始时间和结束时间之间的时差(这认为结果字符串将与上述正则表达式匹配:

To get the time difference between start and end times (this considers that the resulting string will be a match of the above regex:

extension StringProtocol {
    var minutesFromTime: Int {
        let index = firstIndex(of: ":")!
        return Int(self[..<index])! * 60 + Int(self[index...].dropFirst())!
    }
}


let start = response[Range(match.range(at: 1), in: response)!]
let end = response[Range(match.range(at:2), in: response)!]
let minutes = end.minutesFromTime - start.minutesFromTime // 540
let hours = Double(minutes) / 60  // 9

这篇关于Swift 5:如何根据提供的字符串计算正确的Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆