ios 获取当前年月日
在iOS中,可以使用NSCalendar和NSDateComponents来获取当前的年、月和日。
let calendar = NSCalendar.current
let components = calendar.dateComponents([.year, .month, .day], from: Date())
let year = components.year
let month = components.month
let day = components.day
在上述代码中,首先创建一个NSCalendar对象,然后使用dateComponents(_:from:)方法从当前的Date对象中获取指定的日期组件。在这里,我们指定了需要获取的年、月和日的日期组件。
最后,通过components对象的属性获取当前的年、月和日。
原文地址: https://www.cveoy.top/t/topic/iYne 著作权归作者所有。请勿转载和采集!