ios - 如何将当前月份作为字符串获取?

标签 ios swift date nsdate nscalendar

<分区>

我需要获取当前月份的可能,但我做不到。我怎样才能做到这一点?

   let date = NSDate()
   let calendar = NSCalendar.currentCalendar()
   let components = calendar.components([.Day , .Month , .Year], fromDate: date)
   
   let year =  components.year
   let month = components.month
   let day = components.day

我这样做了,但没有用。

最佳答案

let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "LLLL"
let nameOfMonth = dateFormatter.string(from: now)

关于ios - 如何将当前月份作为字符串获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820798/

相关文章:

ios - 当应用程序在 ios 中处于后台时,在主屏幕上显示 View

iphone - 以编程方式实现 UIScrollView

ios - IBDesignable UI 未在带有 Swift 的 Xcode 9 中显示

swift - 为什么我的 endUpdates 抛出错误(当它在同一个函数中工作时)

php - 使用 PHP 将日期和时间保存到 MySQL 时遇到问题

ios - 无法在强制转换中将类型 '[Record]' 的值转换为类型 'Record'

ios - 在 iOS Swift 中使用正则表达式提取值

ios - Swift:将容器 View 中的几个标签设置为容器的比率/百分比

python - 如何将日期传递给 Python 中的脚本?

php - 如何搜索mysql DATE并返回结果以及日期和数据?