swift – NSDateFormatter.stringFromDate(NSDate())返回空字符串

swift – NSDateFormatter.stringFromDate(NSDate())返回空字符串,第1张

概述在下面的图片中,您可以看到我写的代码和所有变量的值: class fun getCurrentShortDate() -> String { var todaysDate = NSDate() var dateFormatter = NSDateFormatter() dateFormatter.dateFormate = "dd-MM-yyyy" var Date 在下面的图片中,您可以看到我写的代码和所有变量的值:
class fun getCurrentShortDate() -> String {    var todaysDate = NSDate()    var dateFormatter = NSDateFormatter()    dateFormatter.dateFormate = "dd-MM-yyyy"    var Dateinformat = dateFormatter.stringFromDate(todaysDate)    return Dateinformat}@H_301_2@  

Variable values

正如你可以看到当前日期被发现没有问题,但是当我尝试将NSDate更改为字符串时,它不会这样做.

更新:Xcode 8.2•Swift 3.0.2
extension DateFormatter {    convenIEnce init(dateStyle: Style) {        self.init()        self.dateStyle = dateStyle    }    convenIEnce init(timeStyle: Style) {        self.init()        self.timeStyle = timeStyle    }    convenIEnce init(dateStyle: Style,timeStyle: Style) {        self.init()        self.dateStyle = dateStyle        self.timeStyle = timeStyle    }}@H_301_2@  
extension Date {    static let shortDate = DateFormatter(dateStyle: .short)    static let fullDate = DateFormatter(dateStyle: .full)    static let shortTime = DateFormatter(timeStyle: .short)    static let fullTime = DateFormatter(timeStyle: .full)    static let shortDateTime = DateFormatter(dateStyle: .short,timeStyle: .short)    static let fullDateTime = DateFormatter(dateStyle: .full,timeStyle: .full)    var fullDate:  String { return Date.fullDate.string(from: self) }    var shortDate: String { return Date.shortDate.string(from: self) }    var fullTime:  String { return Date.fullTime.string(from: self) }    var shortTime: String { return Date.shortTime.string(from: self) }    var fullDateTime:  String { return Date.fullDateTime.string(from: self) }    var shortDateTime: String { return Date.shortDateTime.string(from: self) }}@H_301_2@  

测试

print(Date().fullDate)  // "FrIDay,May 26,2017\n"print(Date().shortDate)  // "5/26/17\n"print(Date().fullTime)  // "10:16:24 AM Brasilia Standard Time\n"print(Date().shortTime)  // "10:16 AM\n"print(Date().fullDateTime)  // "FrIDay,2017 at 10:16:24 AM Brasilia Standard Time\n"print(Date().shortDateTime)  // "5/26/17,10:16 AM\n"@H_301_2@          总结       

以上是内存溢出为你收集整理的swift – NSDateFormatter.stringFromDate(NSDate())返回空字符串全部内容,希望文章能够帮你解决swift – NSDateFormatter.stringFromDate(NSDate())返回空字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1026195.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存