2016-09-27 42 views
1

我想在我的iOS應用程序中使用年度季度,但發現.quarter組件什麼都不做。我也沒有找到關於它的文檔。Calender.Component中的Quarter Component是做什麼的? (Swift)

調用print(Calendar.current.date(byAdding: .quarter, value: 1, to: Date()))只是打印當前日期。我預計它會增加3個月呢?

+1

該季度組件似乎是越野車,這裏是另一個報告:http://stackoverflow.com/questions/11618784/i-want-to-get-a-quarter-value-in-nsdatecomponents-class。 –

+0

@MartinR哇,那是4年前。你會認爲他們會注意到,現在可能已經修好了...... – calebmanley

回答

3

你會認爲你不會 - 但不會,看起來有些東西壞了。

在操場的快速測試顯示它的所有作品,直到您使用.quarter:在

//: Playground - noun: a place where people can play 

import UIKit 

let result = Calendar.current.date(byAdding: .quarter, value: 1, to: Date()) 

print (result) 

let calendar1 = Calendar.current 
let now1 = Date() 
let newDate1 = calendar1.date(byAdding: .minute, value: 1, to: now1) 
let newDate2 = calendar1.date(byAdding: .hour, value: 1, to: now1) 
let newDate3 = calendar1.date(byAdding: .day, value: 1, to: now1) 
let newDate4 = calendar1.date(byAdding: .month, value: 1, to: now1) 
let newDate5 = calendar1.date(byAdding: .month, value: 3, to: now1) 
let newDate6 = calendar1.date(byAdding: .quarter, value: 1, to: now1) 

結果:

"Sep 27, 2016, 3:30 PM" 

"Optional(2016-09-27 05:30:03 +0000)\n" 

gregorian (current) 
"Sep 27, 2016, 3:30 PM" 
"Sep 27, 2016, 3:31 PM" 
"Sep 27, 2016, 4:30 PM" 
"Sep 28, 2016, 3:30 PM" 
"Oct 27, 2016, 3:30 PM" 
"Dec 27, 2016, 3:30 PM" 
"Sep 27, 2016, 3:30 PM" 

它看起來像,因爲它矗立在Xcode 8.0(8A218a)你將使用上述newDate5行中顯示的月份和值3

+0

這是不幸的。使我的代碼從一個開始日期計算未來的季度帳單日期比它應該更復雜... – calebmanley

+0

同樣的問題在這裏,除了添加.res,temp temparound:使用3 .month – mnl

相關問題