2014-07-14 169 views
2

我有一個SQL內置的SQL有2個參數,一個是年份,另一個是月份。TDateTimePicker delphi獲取年份和月份

在delphi中,我在框架上添加了一個TDateTimePicker,以便讓用戶選擇月份和年份。

如何從TDateTimePicker中檢索Year/Day的值?

我試過DateTimePicker2.Date;但是這給了我一個日期格式 「XX/XX/XXXX」

我想要得到的實際年/月

int year ; 
year := DateTimePicker2.Date.Year; 

有什麼辦法可以做到這一點?

+2

使用['YearOf'](http://docwiki.embarcadero.com/Libraries/XE6/en/System.DateUtils.YearOf)和['MonthOf'](http: //docwiki.embarcadero.com/Libraries/XE6/en/System.DateUtils.MonthOf)函數。或者['DecodeDate'](http://docwiki.embarcadero.com/Libraries/XE6/en/SystemUtils.DecodeDate)立即獲取它們。 – TLama

+1

謝謝,先生!我知道有這樣做的功能,但我找不到它們。 – CiucaS

+0

你想讓這個答案@TLama? –

回答

5

正如TLama說,你可以使用DecodeDate功能如下: -

Var 
    lDay, 
    lMonth, 
    lYear : Word; 
Begin 
    DecodeDate(DateTimePicker2.Date, lDay, lMonth, lYear); 
    MyStoredProc.Params[0].AsInteger := lDay; 
    MyStoredProc.Params[1].AsInteger := lMonth; 
    MyStoredProc.Execute; 
End; 
3

使用YearOf()MonthOf()功能DateUtils

您的代碼將是:

year := YearOf(DateTimePicker1.Date); 
month := MonthOf(DateTimePicker1.Date); 

如果該值在DateTimePicker1.Date27/8/2015 那麼值年份爲2015年,月份爲8.