日期将无法正确显示
Date will not display correctly
我正在尝试让我的日期显示如 "March 2nd, 2018 10:00pm"。我尝试了 "MM-dd-yyyy" 和 "yyyy-MM-dd hh:mm:ss" 但似乎 none 这些组合正在获取日期 我 desire.The 函数选择日期是 sendRequest 并且它正在使用 UIDatePicker。
func getCurrentDateTimeFromTimeStamp(timestamp:String)->String{
let date = NSDate(timeIntervalSince1970:Double(timestamp)!)
let formatter = DateFormatter()
formatter.dateFormat = "MMMM d, yyyy HH:mm a"
return formatter.string(from: date as Date)
}
let dateCellVar = request.timestamp
let dateString = dateCellVar.description
dateCell.textLabel?.text = self.getCurrentDateTimeFromTimeStamp(timestamp: dateString)
class Request {
var timestamp:Double
init(dict: [String: Any]) {
self.timestamp = dict["timestamp"] as? Double ?? 0.0
}
func sendRequest(){
print("HEY:")
guard let user = currentUser else { return }
print("USER: \(user.firstLastName)")
if let da = dateField.text{
print(da)
}
print(timeField.text)
print(locationField.text)
print(messageTextView.text)
guard let pickedDate = pickedDate else { return print("pickedDate") }
guard let date = dateField.text else { return print("pickedDate") }
guard let time = timeField.text else { return print("time") }
guard let location = locationField.text else { return print("location")}
let db = Database.database().reference()
let ref = db.child("requests").childByAutoId()
let data = [
"sender": user.uid,
"recipient": recipientUser.uid,
"name": user.firstLastName,
"photoURL": user.photoURL,
"location": location,
"date": date,
"time": time,
"pickedTimestamp": pickedDate.timeIntervalSince1970,
"message": messageTextView.text ?? "",
"status": "PENDING",
"timestamp": [".sv": "timestamp"]
] as [String:Any]
print("HEYO")
ref.setValue(data) { error, ref in
if error == nil {
print("Success")
} else {
print("Failed")
}
}
}
根据您的结果,您的时间戳以毫秒为单位,而不是秒。您需要除以 1000。
你也有错dateFormat
。您希望在一小时内使用 hh
,而不是 HH
。 H
用于 24 小时制,这在使用 a
表示 AM/PM 时没有任何意义。您还应该避免使用 dateFormat
。使用 dateStyle
和 timeStyle
。让格式化程序为您提供最适合用户区域设置的日期格式。
您的代码还做了很多不必要的转换。您将时间戳记为 Double
并将其存储为 Double
。但是然后你的函数将你期望的秒数转换为 String
的时间戳,然后你将其转换回 Double
。避免不必要地使用 String
.
要获取序数日,您可以使用 Calendar
从 Date
中提取 day
。
let date = Date()
let calendar = Calendar.current
let dateComponents = calendar.component(.day, from: date)
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .ordinal
let day = numberFormatter.string(from: dateComponents as NSNumber)
从那里开始,您只需使用 DateFormatter
对日期格式进行编码,然后放入上面提取的序号日期,如下所示:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM '\(String(describing: day!)),' yyyy h:mm a"
let dateString = "\(dateFormatter.string(from: date))"
print(dateString)
此外,您的年份问题可能源于您提供的数字。 timeIntervalSince1970
需要整整几秒,所以请确保你喂它整整几秒。
init(timeIntervalSince1970: TimeInterval) Returns a date object
initialized relative to 00:00:00 UTC on 1 January 1970 by a given
number of seconds.
改编自 this answer. Additionally, you may find this site 有助于格式化日期。
我正在尝试让我的日期显示如 "March 2nd, 2018 10:00pm"。我尝试了 "MM-dd-yyyy" 和 "yyyy-MM-dd hh:mm:ss" 但似乎 none 这些组合正在获取日期 我 desire.The 函数选择日期是 sendRequest 并且它正在使用 UIDatePicker。
func getCurrentDateTimeFromTimeStamp(timestamp:String)->String{
let date = NSDate(timeIntervalSince1970:Double(timestamp)!)
let formatter = DateFormatter()
formatter.dateFormat = "MMMM d, yyyy HH:mm a"
return formatter.string(from: date as Date)
}
let dateCellVar = request.timestamp
let dateString = dateCellVar.description
dateCell.textLabel?.text = self.getCurrentDateTimeFromTimeStamp(timestamp: dateString)
class Request {
var timestamp:Double
init(dict: [String: Any]) {
self.timestamp = dict["timestamp"] as? Double ?? 0.0
}
func sendRequest(){
print("HEY:")
guard let user = currentUser else { return }
print("USER: \(user.firstLastName)")
if let da = dateField.text{
print(da)
}
print(timeField.text)
print(locationField.text)
print(messageTextView.text)
guard let pickedDate = pickedDate else { return print("pickedDate") }
guard let date = dateField.text else { return print("pickedDate") }
guard let time = timeField.text else { return print("time") }
guard let location = locationField.text else { return print("location")}
let db = Database.database().reference()
let ref = db.child("requests").childByAutoId()
let data = [
"sender": user.uid,
"recipient": recipientUser.uid,
"name": user.firstLastName,
"photoURL": user.photoURL,
"location": location,
"date": date,
"time": time,
"pickedTimestamp": pickedDate.timeIntervalSince1970,
"message": messageTextView.text ?? "",
"status": "PENDING",
"timestamp": [".sv": "timestamp"]
] as [String:Any]
print("HEYO")
ref.setValue(data) { error, ref in
if error == nil {
print("Success")
} else {
print("Failed")
}
}
}
根据您的结果,您的时间戳以毫秒为单位,而不是秒。您需要除以 1000。
你也有错dateFormat
。您希望在一小时内使用 hh
,而不是 HH
。 H
用于 24 小时制,这在使用 a
表示 AM/PM 时没有任何意义。您还应该避免使用 dateFormat
。使用 dateStyle
和 timeStyle
。让格式化程序为您提供最适合用户区域设置的日期格式。
您的代码还做了很多不必要的转换。您将时间戳记为 Double
并将其存储为 Double
。但是然后你的函数将你期望的秒数转换为 String
的时间戳,然后你将其转换回 Double
。避免不必要地使用 String
.
要获取序数日,您可以使用 Calendar
从 Date
中提取 day
。
let date = Date()
let calendar = Calendar.current
let dateComponents = calendar.component(.day, from: date)
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .ordinal
let day = numberFormatter.string(from: dateComponents as NSNumber)
从那里开始,您只需使用 DateFormatter
对日期格式进行编码,然后放入上面提取的序号日期,如下所示:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM '\(String(describing: day!)),' yyyy h:mm a"
let dateString = "\(dateFormatter.string(from: date))"
print(dateString)
此外,您的年份问题可能源于您提供的数字。 timeIntervalSince1970
需要整整几秒,所以请确保你喂它整整几秒。
init(timeIntervalSince1970: TimeInterval) Returns a date object initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.
改编自 this answer. Additionally, you may find this site 有助于格式化日期。