我有代码:
.dropdown-menu {
opacity: 0;
position: absolute;
top: 35px;
visibility: hidden;
-webkit-transition: all .25s ease;
-moz-transition: all .25s ease;
-ms-transition: all .25s ease;
-o-transition: all .25s ease;
transition: all .25s ease;
}
nav li:hover .dropdown-menu {
opacity: 1;
top: 50px;
visibility: visible;
}
但是dateInTheMiddleOfPeriod是2015- 11 -18 06:00:12 +0000,但我希望2015- 12 -18 06:00:12 +0000,因为我需要上个月的日期。为什么会这样?
答案 0 :(得分:2)
您应该使用NSCalendar方法日期,通过添加单位从今天减去一个月并使用NSCalendar方法组件获取月份值如下:
var previousMonth: Int {
let cal = NSCalendar.autoupdatingCurrentCalendar()
return cal.component(.Month, fromDate: cal.dateByAddingUnit(.Month, value: -1, toDate: NSDate(), options: [])!)
}
previousMonth // 12
答案 1 :(得分:1)
要获得一个月的日期,然后才能按照步骤操作,
示例代码:
let components = NSCalendar.currentCalendar().components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: NSDate())
components.month -= 1
let dateInTheMiddleOfPeriod = NSCalendar.currentCalendar().dateFromComponents(components)!
print(dateInTheMiddleOfPeriod)