2017-03-01 194 views
0

我有一個蓮花筆記數據庫。在表單中有一個名爲「ServiceDate」的字段,另一個字段名爲「ReadOrEdit」。計算兩個日期之間有多少個月來定義它是6個月還是6個月以上

的「ServiceDate」字段我需要時間來存儲,所以我用

@Date(@ThisValue) 

妥善存放日期。

下面是這種情況

如果ServiceDate是6個月內,該ReadOrEdit字段將顯示「編輯」,如果ServiceDate超過6個月,ReadOrEdit字段將顯示「只讀」。那時候,用戶還沒有確定6個月的確切時間,所以我假設6個月大致有186(31 * 6)天。

在「ReadOrEdit」領域,我把下面的代碼

SixMonths := 186; 
date := @Now; 
calculation := (@Date(date)[email protected](ServiceDate))/((60*60*24) + 1); 
result := @If(calculation > SixMonths;"ReadOnly";"Editable"); 
@If(@IsError(result);"no result";result) 

當我提出這種做法給用戶,用戶拒絕我的做法。用戶認爲我不應該用天來定義6個月和6個月應每月定義,例如,今天是2017年1月3日,6個月前,即2016年1月9日。如果日期在01/09/2016至01/03/2017之間,則ReadOrEdit字段應該顯示「可編輯」。如果日期在01/09/2016之前,則ReadOrEdit字段應該顯示「ReadOnly」。

website激勵我在ReadOrEdit領域使用@Adjust。我使用以下代碼嘗試使用@Adjust查找ServiceDate之前6個月的日期。

sixMonths :[email protected](@Adjust(ServiceDate;0;-6;0;0;0;0)); 
@If(@IsError(sixMonths);"no result";sixMonths) 

當我運行程序時,如果輸入的ServiceDate是01/03/2017,ReadOrEdit字段將顯示01/09/2016。由於結果與用戶要求相同,我相信我應該在程序中使用@Adjust。我需要考慮接下來的事情是如何計算有多少今天和ServiceDate

我在互聯網上檢索算法之間的月不是兩個日期之間計算月多的信息。我認爲代碼可能類似於我提出代碼,所以我開始修改代碼,以滿足用戶reqirement。

下面是代碼

today :[email protected]; 
sixMonths :[email protected](@Adjust(ServiceDate;0;-6;0;0;0;0)); 
sixMonthDays := @Abs(@Integer((@Date(Today) - @Date(ServiceDate)))); 
calculation:=(@Date(today)[email protected](sixMonths)); 
result := @If(calculation > sixMonthDays;"ReadOnly";"Editable"); 
@If(@IsError(result);"no result";result) 

當我運行程序時,我注意到結果是不正確的。

這裏是我的attemtps,結果只顯示「只讀」不管日期是6個月內或日期是6個月以上。

如果輸入ServiceDate是2017年1月3日,它示出了「只讀」

如果輸入ServiceDate是28/02/2017,它示出了「只讀」

如果輸入ServiceDate是01/09/2016,它示出了「只讀」

如果輸入ServiceDate是2016年2月9日,它示出了「只讀」

如果輸入ServiceDate是2016年2月8日,它示出了「只讀」

如果我的代碼

result := @If(calculation > sixMonthDays;"ReadOnly";"Editable"); 

改變

result := @If(calculation < sixMonthDays;"ReadOnly";"Editable"); 

結果仍然是不正確的。

這裏是我的嘗試,結果只顯示「可編輯」,無論日期是在6個月內還是日期超過6個月。

如果輸入ServiceDate是2017年1月3日,它示出了「可編輯」

如果輸入ServiceDate是28/02/2017,它示出了「可編輯」

如果輸入ServiceDate是01/09/2016,它示出了「可編輯」

如果輸入ServiceDate是2016年2月9日,它示出了「可編輯」

如果輸入ServiceDate是2016年2月8日,它示出了「可編輯」

其實我覺得最終的結果應該是這樣的

如果輸入ServiceDate是2017年1月3日,「ReadOrEdit」欄顯示「編輯」

如果輸入ServiceDate爲28/02/2017,「 ReadOrEdit」字段示出了‘可編輯’

如果輸入ServiceDate是2016年1月9日,‘ReadOrEdit’字段示出了‘可編輯’

如果輸入ServiceDate是2016年2月9日,‘ReadOrEdit’字段示出了「ReadOnly」

如果t他輸入ServiceDate是02/08/2016,「ReadOrEdit」字段顯示「ReadOnly」

我修改了代碼,我認爲在代碼中有錯誤,但我知道在哪個部分。我讀了這個post,但我不明白。

如果有人讓我知道我的錯誤或讓我知道如何計算兩個日期(今天和ServiceDate)之間的多少個月,以便我可以設置它是可編輯的還是隻讀的,請予以感謝。非常感謝你。

回答

2

如果我瞭解你,在服務日期後的六個月內可以編輯一些東西?

您可以直接比較日期。

@If(@Adjust(ServiceDate; 0; 6; 0; 0; 0; 0) >= @Today; "Editable"; "ReadOnly")

這假定ServiceDate是日期/時間值。

如果ServiceDate小於或剛好在6個月前,則應返回「可編輯」,否則如果ServiceDate超過6個月前返回「ReadOnly」。 @Today與@Now相似,只是它不包含一天中的時間而只是一個僅限日期的值。

+0

在我收到你的回答之前,我仍然在想哪個部分有錯誤。你的回答很明確,很有幫助。非常感謝你。 – beginner