ios - Swift 3.0 Xcode8 beta 4 如何修复 Calendar.current.date
<p><p>晚上,我真的不知道如何转换:</p>
<pre><code>let birthDay = Calendar.current.date(era: 1, year: year, month: moth, day: day, hour: 0, minute: 0, second: 0, nanosecond: 0)
</code></pre>
<p>进入这个:</p>
<pre><code>Calendar.current.date(from: <DateComponents>)
</code></pre>
<p>有什么建议吗?</p>
<p>这就是我所做的:</p>
<pre><code>let dateComponet = DateComponents(timeZone: nil, era: 1, year: year, month: month, day: day, hour: 0, minute: 0, second: 0, nanosecond: 0)
let birthDay = Calendar.current.date(from: dateComponet)
</code></pre>
<p>有没有更短的方法?</p>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>是的,您可以在使用该初始化程序时仅传递您想要的组件,以及当前日历并从 datecomponents 对象获取日期属性:</p>
<p><strong>日期组件</strong></p>
<pre><code>public init(calendar: Calendar? = default,
timeZone: TimeZone? = default,
era: Int? = default,
year: Int? = default,
month: Int? = default,
day: Int? = default,
hour: Int? = default,
minute: Int? = default,
second: Int? = default,
nanosecond: Int? = default,
weekday: Int? = default,
weekdayOrdinal: Int? = default,
quarter: Int? = default,
weekOfMonth: Int? = default,
weekOfYear: Int? = default,
yearForWeekOfYear: Int? = default)
</code></pre>
<hr/>
<pre><code>let birthDay = DateComponents(calendar: .current, year: 2016, month: 8, day: 9).date! // "Aug 9, 2016, 12:00 AM"
</code></pre></p>
<p style="font-size: 20px;">关于ios - Swift 3.0 Xcode8 beta 4 如何修复 Calendar.current.date,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38837892/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38837892/
</a>
</p>
页:
[1]