如何减去日期分量? [英] How to subtract date components?
本文介绍了如何减去日期分量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天是星期五
,根据 NSCalendar
,它是 6
.我可以通过使用以下
As today is Friday
, which is 6
according to NSCalendar
. I can get this by using the following
Calendar.current.component(.weekday, from: Date())
如何获取上周 Saturday
的工作日部分,应该是 7
?
How do I get weekday component of Saturday
last week, which should be 7
?
如果我执行 Calendar.current.component(.weekday,来自:Date())-6
.我收到 0
无效的分量.
If I do Calendar.current.component(.weekday, from: Date()) - 6
. I am getting 0
which is not valid component.
推荐答案
尝试一下,您必须先获取日期,然后再从中减去:
Try this, you have to get the date first then subtract again from it:
var dayComp = DateComponents(day: -6)
let date = Calendar.current.date(byAdding: dayComp, to: Date())
Calendar.current.component(.weekday, from: date!)
这篇关于如何减去日期分量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文