如何在 swift 中显示来自 Date() 的星期几的名称

How to display the name of the day of the week from Date() in swift

我正在 swift 中的一个项目中,我从 Date() 中调用国际格式的日期并将其放入文本标签中。我让它工作,但我还需要它在它前面说出星期几的名称,例如:

2017 年 5 月 11 日,星期四

我不知道 dd-MMM-yyyy 格式可以做到这一点,如果有的话。我已经搜索过了,但我没有找到任何东西。我是 swift 的新手,非常感谢您的帮助。

到目前为止,我的代码如下所示。在视图控制器中,我在视图控制器 class.

下方有一个标签出口
         @IBOutlet weak var dateDisplay: UILabel!

然后在覆盖 viewDidLoad() 函数中,我有将系统日期格式化为国际格式的代码

let date = Date()
    let formatter = DateFormatter()
    formatter.dateFormat = "dd-MMM-yyyy"
    let result = formatter.string(from: date)
    dateDisplay.text! = result
    todayDate = result

然后在视图控制器之外 class 但仍在视图控制器中我有一个变量

 var todayDate: String = ("")

现在它将“11-May-2017”放入标签中。 我想将 "Thursday-11-May-2017" 放入标签中。有什么办法吗? 非常感谢。

使用此格式按照您想要的方式进行格式化,

let date = Date()
let format = "EEEE-dd-MMM-yyyy"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format

dateFormatter.string(from: date)