Ios 如何正确设置日期格式
如何格式化此日期以使其可读。我希望它显示例如:“2018年1月7日上午7:30”。我试图查看其他答案,但不确定在哪里添加扩展Ios 如何正确设置日期格式,ios,swift,nsdateformatter,Ios,Swift,Nsdateformatter,如何格式化此日期以使其可读。我希望它显示例如:“2018年1月7日上午7:30”。我试图查看其他答案,但不确定在哪里添加扩展 let date = Date(timeIntervalSince1970: request.timestamp / 1000) dateCell.textLabel?.text = date.description class Request { var key:String var sender:String var recipient:Stri
let date = Date(timeIntervalSince1970: request.timestamp / 1000)
dateCell.textLabel?.text = date.description
class Request {
var key:String
var sender:String
var recipient:String
var name:String
var location:String
var when:String
var whereStr:String
var message:String
var timestamp:Double
var status:String
init(dict: [String: Any]) {
self.key = dict["key"] as? String ?? ""
self.sender = dict["sender"] as? String ?? ""
self.recipient = dict["recipient"] as? String ?? ""
self.name = dict["name"] as? String ?? ""
self.location = dict["location"] as? String ?? ""
self.when = dict["when"] as? String ?? ""
self.whereStr = dict["where"] as? String ?? ""
self.message = dict["message"] as? String ?? ""
self.timestamp = dict["timestamp"] as? Double ?? 0.0
self.status = dict["status"] as? String ?? ""
}
您应该使用DateFormatter:
let formatter = DateFormatter()
formatter.format = "MMMM d, yyyy HH:mm a"
let dateString = formatter.string(from: date)
试试这个
func getCurrentDateTimeFromTimeStamp(timeStapm:String)->String{
let date = NSDate(timeIntervalSince1970:Double(timeStapm)!/1000)
let formatter = DateFormatter()
formatter.dateFormat = "MMMM d, yyyy HH:mm a"
return formatter.string(from: date as Date)
}
根据您的要求传递时间戳并以字符串格式获取日期,只需传递日期格式即可
像这样使用
dateCell.textLabel?.text = self.getCurrentDateTimeFromTimeStamp(timeStapm:"pass your timestamp")
您可以在应用程序中的任何位置添加扩展名,并为comlpete应用程序添加一个(同名)扩展名。往下看
import UIKit
class ViewControllerHellBoy: UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dateCell.textLabel?.text = self.userFriendlyFullDate()
}
}
extension Date: Dateable {
var formatter: DateFormatter { return DateFormatter() }
/** Return a user friendly hour */
func userFriendlyFullDate() -> String {
// Customize a date formatter
formatter.dateFormat = ""MMMM d, yyyy HH:mm a"" //Use format you want to
formatter.timeZone = TimeZone(abbreviation: "UTC") . // Time zone you wishes to provide
return formatter.string(from: self)
}
这就是如何使用扩展。您还可以创建单独的类并将所有扩展保留在其中。使用dateFormatter对象根据需要设置日期样式
let format = DateFormatter()
format.format = "MM/dd/yyyy"
//get your date in string like this
let dateInString = format.string(from: date)
// this will give you "10/02/2018"
您可以访问以查看不同的格式以及如何获取它们。为什么需要将数字除以1000?可能是因为时间戳以毫秒而不是秒为单位。我会这样使用它吗?dateCell.textLabel?.text=getCurrentDateTimeFromTimeStamp(timestamp:request.timestamp)返回为“650081年6月27日23:21 PM”,有没有办法修复它以正确显示年份和时间?我只是不明白如何在我的dateCell.textLabel?.textBig警告,如果您没有将区域设置为
en_US_POSIX
,然后,系统区域设置将覆盖12/24小时格式。