很抱歉,如果之前有人询问过。
我有一个来自MySQL的日期填充Swift中的tableview。我正在将MySQL日期转换为NSDate
static
这会生成正确的日期,但现在我需要将输出更改为更像:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = "\(dateLabelSource)"
if let dateAdded = dateFormatter.dateFromString(dateString)
{
self.dateLabel.text = "\(dateAdded)"
}
但无法解决如何做到这一点。
感谢任何帮助。
答案 0 :(得分:2)
这应该产生你想要的输出。注意:NSDateFormatters是要创建的昂贵对象,因此最好尽可能多地重用单个实例。
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = "\(dateLabelSource)"
if let dateAdded = dateFormatter.dateFromString(dateString)
{
dateFormatter.dateFormat = "EEEE d MMMM yyyy"
self.dateLabel.text = "\(dateFormatter.stringFromDate(dateAdded))"
}
“数据格式指南”中有一节涉及格式化日期。 Data Formatting Guide
答案 1 :(得分:0)
这将有助于
class Feed: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var table: UITableView!
@IBOutlet var feedBar: UINavigationBar!
var titles = [String]()
var messages = [String]()
var usernames = [String]()
var types = [String]()
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
messages.reverse()
titles.reverse()
usernames.reverse()
types.reverse()
}
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
override func viewDidAppear(animated: Bool) {
println("View appeared")
self.messages.removeAll(keepCapacity: true)
self.titles.removeAll(keepCapacity: true)
self.usernames.removeAll(keepCapacity: true)
self.types.removeAll(keepCapacity: true)
var postQuery = PFQuery(className: "Post")
postQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.messages.append(object["message"] as! String)
self.titles.append(object["title"] as! String)
self.usernames.append(object["username"] as! String)
self.types.append(object["type"] as! String)
self.table.reloadData()
}
}
})
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let postCell = tableView.dequeueReusableCellWithIdentifier("feedPost", forIndexPath: indexPath) as! PostCell
var post = PFObject(className: "Post")
if messages.isEmpty == false {
postCell.message.text = messages[indexPath.row]
}
if titles.isEmpty == false {
postCell.postTtitle.text = titles[indexPath.row]
}
if usernames.isEmpty == false {
postCell.posterName.setTitle(usernames[indexPath.row], forState: .Normal)
}
if self.types[indexPath.row] == "post1" {
postCell.sideLeft.backgroundColor = self.UIColorFromRGB(0xFCFFBD)
postCell.sideRight.backgroundColor = self.UIColorFromRGB(0xFCFFBD)
} else if self.types[indexPath.row] == "post2" {
postCell.sideLeft.backgroundColor = self.UIColorFromRGB(0xFFB4AC)
postCell.sideRight.backgroundColor = self.UIColorFromRGB(0xFFB4AC)
} else if self.types[indexPath.row] == "post3" {
postCell.sideLeft.backgroundColor = self.UIColorFromRGB(0xFFD5A4)
postCell.sideRight.backgroundColor = self.UIColorFromRGB(0xFFD5A4)
}
postCell.selectionStyle = .None
postCell.message.scrollRangeToVisible(NSMakeRange(0, 0))
return postCell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return usernames.count
}
}
答案 2 :(得分:0)
使用此代码格式化日期:
let dateFmt: NSDateFormatter = NSDateFormatter()
dateFmt.dateFormat = "EEEE d MMMM yyyy"
dateFmt.locale = NSLocale(localeIdentifier: "en_US") // If you consider to not localise date
println(dateFmt.stringFromDate(dateAdded))