2014-10-03 40 views
2

我已經有一個邏輯來檢查輸入的日期正是一個財政年度日期更好的邏輯來檢查財政年度日期

if (DateTime.IsLeapYear(endate.Year)) 
{ 
    if (totalDay != 366) 
    { 
     error("Please make sure start and end dates are financial year dates.<br />E.g.<br /> Start Date:01/07/2012 <br /> End Date :30/06/2013"); 
     return; 
    } 
} 
else 
{ 
    if (totalDay != 365) 
    { 
     error("Please make sure start and end Dates are Financial year dates.<br />E.g.<br /> Start Date:01/07/2012 <br /> End Date :30/06/2013"); 
     return; 
    } 
} 

這樣我檢查進入了一個日期都正好滿足1年的差距。但它不檢查用戶是否剛剛輸入startdate 01/07/20XX - endate 30/06/20XX。我可以硬編碼和驗證日期和月份,並檢查輸入年份之間的差異來實現這一目標,但有沒有更好的方法可以做到這一點?

+0

這真的取決於你的觀衆。不同的國家有不同的年度報告期。 – 2014-10-03 00:14:14

+0

當然,但這裏的財政年度日期例如:01/07/2013 - 30/06/2014。現在這個應用程序,並沒有在任何其他國家使用。但是你是對的,爲所有國家設計一個有趣的設計 – 2014-10-03 00:18:43

+0

您可以將財政年度的日期包含在資源文件中。對於不同的國家,您需要創建不同的資源文件。 的winform: http://msdn.microsoft.com/en-us/library/y99d1cd3.aspx Web窗體: http://msdn.microsoft.com/en-us/library/vstudio/ms247246(v = vs.100).aspx – 2014-10-03 01:01:54

回答

0

我希望這將有助於

DateTime starDate = TextToDate(txtStartDate.Text); //01/07/2013 
// or startDate = DateTimePicker1.Value; 

DateTime endDate = TextToDate(txtEndDate.Text); //30/06/2014 
// or endDate = DatetTimePicker2.Value 

DateTime calculatedEndDate = startDate.AddYears(1).AddDays(-1); 
// 01/07/2013 .AddYears(1) ==> 01/07/2014 .AddDays(-1) ==> 30/06/2014 

// Note for above line: 
//  if you will be using the start date and end date in an sql query 
//  remove the call to function AddDays(-1) 
//  for display purposes, keep the function AddDays(-1) 


if(endDate != calculatedEndDate) 
// note: if DateTimePicker is returning time component, then you will need to compare each (year, month and day) 
{ 
    // show error message 
} 
else 
{ 
    // dates are 1 year apart. 
} 

或者,您可以要求用戶只輸入開始日期,然後計算的終止日期。

相關問題