Swift 5:如何根据提供的字符串计算出正确的日期?
Swift 5: How to calculate correct Date from provided string?
我不擅长Swift所以我想知道如何计算当前时间在两次之间
我从后端收到以下响应。
{"workHours":"M-F 9:00 - 18:00"}
如何使用正则表达式和 DateFormatter 从 "M-F 11:00 - 20:00"
响应中获取 start
(9:00) 时间、end
(18:00) 时间?(我不确定是从这种字符串中获取Date对象的正确方法,如果不是请推荐最好的方法和经验)
您可以使用以下正则表达式:
"^\w-\w\s{1}(\d{1,2}:\d{2})\s{1}-\s{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
获取开始时间和结束时间之间的时间差(这认为生成的字符串将匹配上述正则表达式:
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所以我想知道如何计算当前时间在两次之间
我从后端收到以下响应。
{"workHours":"M-F 9:00 - 18:00"}
如何使用正则表达式和 DateFormatter 从 "M-F 11:00 - 20:00"
响应中获取 start
(9:00) 时间、end
(18:00) 时间?(我不确定是从这种字符串中获取Date对象的正确方法,如果不是请推荐最好的方法和经验)
您可以使用以下正则表达式:
"^\w-\w\s{1}(\d{1,2}:\d{2})\s{1}-\s{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
获取开始时间和结束时间之间的时间差(这认为生成的字符串将匹配上述正则表达式:
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