Swift 如何在json数据中拆分包含两个UTC格式日期的字符串
在我的应用程序中,mySwift 如何在json数据中拆分包含两个UTC格式日期的字符串,swift,date,Swift,Date,在我的应用程序中,myJSON包含一个带有两个日期的字符串 我的字符串 "2017-07-22 00:00:00 SSS TO 2017-07-25 00:00:00 SSS" 我希望在tableView的cellForRowAt函数中得到结果: func getTrimmedDates(with dates: String) -> [String]{ return dates.replacingOccurrences(of: "SSS", with: "").component
JSON
包含一个带有两个日期的字符串
我的字符串
"2017-07-22 00:00:00 SSS TO 2017-07-25 00:00:00 SSS"
我希望在tableView的cellForRowAt
函数中得到结果:
func getTrimmedDates(with dates: String) -> [String]{
return dates.replacingOccurrences(of: "SSS", with: "").components(separatedBy: "TO")
}
func getFormattedDates(with dates: [String]) -> String {
let joinedDates = dates.compactMap({ $0.toDate().dateToFormattedString() }).joined(separator: " - ")
return joinedDates
}
- 预期格式:
22 Jul - 25 Jul
let dict1 = fiidiiOverviewArr[section].date
let items = dict1?.components(separatedBy: " TO ")
let date1 = items![0]
let date2 = items![1]
print(date1)
print(date2)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM '-' dd MMM"
let string = date1 + " - " + date2
let finalDate = dateFormatter.date(from: string)
print(finalDate)
view.Header_date.text = "\(String(describing: finalDate))"
您可以为此创建
日期
和字符串
扩展名:
日期扩展名:
extension Date {
func dateToFormattedString() -> String {
var formatter: DateFormatter?
formatter = DateFormatter()
/// "dd MMM" is your expected format, you can use whatever you want
formatter?.dateFormat = "dd MMM"
var returnValue: String?
returnValue = formatter?.string(from: self)
return "\(returnValue ?? "")"
}
}
extension String {
func toDate() -> Date {
var formatter: DateFormatter?
formatter = DateFormatter()
formatter?.timeZone = TimeZone(secondsFromGMT: 0)
formatter?.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let date: Date = formatter?.date(from: self) {
return date
} else {
return Date()
}
}
}
let dates = "2017-07-22 00:00:00 SSS TO 2017-07-25 00:00:00 SSS"
let date = getTrimmedDates(with: dates)
let formattedDate = getFormattedDates(with: date)
print(formattedDate) /// You get "22 Jul - 25 Jul"
StringExtension:
extension Date {
func dateToFormattedString() -> String {
var formatter: DateFormatter?
formatter = DateFormatter()
/// "dd MMM" is your expected format, you can use whatever you want
formatter?.dateFormat = "dd MMM"
var returnValue: String?
returnValue = formatter?.string(from: self)
return "\(returnValue ?? "")"
}
}
extension String {
func toDate() -> Date {
var formatter: DateFormatter?
formatter = DateFormatter()
formatter?.timeZone = TimeZone(secondsFromGMT: 0)
formatter?.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let date: Date = formatter?.date(from: self) {
return date
} else {
return Date()
}
}
}
let dates = "2017-07-22 00:00:00 SSS TO 2017-07-25 00:00:00 SSS"
let date = getTrimmedDates(with: dates)
let formattedDate = getFormattedDates(with: date)
print(formattedDate) /// You get "22 Jul - 25 Jul"
然后您应该创建修剪功能:
func getTrimmedDates(with dates: String) -> [String]{
return dates.replacingOccurrences(of: "SSS", with: "").components(separatedBy: "TO")
}
func getFormattedDates(with dates: [String]) -> String {
let joinedDates = dates.compactMap({ $0.toDate().dateToFormattedString() }).joined(separator: " - ")
return joinedDates
}
您应该创建格式化程序函数:
func getTrimmedDates(with dates: String) -> [String]{
return dates.replacingOccurrences(of: "SSS", with: "").components(separatedBy: "TO")
}
func getFormattedDates(with dates: [String]) -> String {
let joinedDates = dates.compactMap({ $0.toDate().dateToFormattedString() }).joined(separator: " - ")
return joinedDates
}
用法:
extension Date {
func dateToFormattedString() -> String {
var formatter: DateFormatter?
formatter = DateFormatter()
/// "dd MMM" is your expected format, you can use whatever you want
formatter?.dateFormat = "dd MMM"
var returnValue: String?
returnValue = formatter?.string(from: self)
return "\(returnValue ?? "")"
}
}
extension String {
func toDate() -> Date {
var formatter: DateFormatter?
formatter = DateFormatter()
formatter?.timeZone = TimeZone(secondsFromGMT: 0)
formatter?.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let date: Date = formatter?.date(from: self) {
return date
} else {
return Date()
}
}
}
let dates = "2017-07-22 00:00:00 SSS TO 2017-07-25 00:00:00 SSS"
let date = getTrimmedDates(with: dates)
let formattedDate = getFormattedDates(with: date)
print(formattedDate) /// You get "22 Jul - 25 Jul"
您可以使用
.components(分隔为:“TO”)
将两个日期放入一个数组中。然后,您可以使用日期格式化程序将每个日期设置为正确的可读格式(e.x.7月22日)。然后,您可以将两个日期字符串连接为“-”。您能给我一个示例吗?实际上,json格式是:“日期”:“2019-07-22 00:00:00.000至2019-07-27 00:00:00.000”我希望7月22日至7月27日您可以修改此扩展,但我会为您的利益编辑我的答案。这给了我7月23日至7月23日根据json数据我得到正确的第二个日期,但我在所有日期中得到的第一个日期您应该设置formatter?.timeZone=timeZone(secondsFromGMT:0)
在toDate()函数中(字符串扩展名)