简体   繁体   English

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

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

I am working on a project in swift where I call the international formatted date from Date() and put it into a text label. 我正在研究swift中的一个项目,我从Date()调用国际格式化日期并将其放入文本标签中。 I got it to work, but I need it to also say the name of the day of the week in front of it, for example: 我得到它的工作,但我需要它也说出它前面的一周的名称,例如:

Thursday, 11-May-2017 2017年5月11日星期四

and I don't know the dd-MMM-yyyy format to do that, if there is one. 如果有的话,我不知道dd-MMM-yyyy这样做的格式。 I have searched for it but I have not found anything. 我已经搜索过但我还没找到任何东西。 I am new to swift and would greatly appreciate some assistance. 我是新手,非常感谢一些帮助。

So far, my code looks like this. 到目前为止,我的代码看起来像这样。 In the view controller I have an outlet to the label just under the view controller class. 在视图控制器中,我在视图控制器类下面有一个标签出口。

         @IBOutlet weak var dateDisplay: UILabel!

Then in the override viewDidLoad() function, I have my the code that formats the system date to the international form 然后在覆盖viewDidLoad()函数中,我有我的代码,将系统日期格式化为国际表单

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

Then outside of the view controller class but still in the view controller I have a variable 然后在视图控制器类之外但仍然在视图控制器中我有一个变量

 var todayDate: String = ("")

Right now it puts "11-May-2017" into the label. 现在它将“2017年5月11日”放入标签中。 I want to put "Thursday-11-May-2017" into the label. 我想把“2017年5月11日星期四”放入标签。 Is there any way to do that? 有没有办法做到这一点? Thank you very much. 非常感谢你。

Use this format to format it in the way you want, 使用此格式以您希望的方式格式化,

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

dateFormatter.string(from: date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM