2011-04-27 34 views
2

我想計算兩個日期之間的差異,並想根據考慮所有月份的差異將其轉換爲2年,5個月或僅3個月,或2天是30天...如何計算兩個不同的日期,如2年,5個月

例如;

和包括::2009年3月12日 ,但不包括:2011年11月26日

的輸出必須爲:2年8個月,14天不包括結束日期。

另一個例子;

開始:2010年1月26日 結束:2010年2月15日

輸出:從開始日期到結束日起20日內,但不包括結束日期

我可以計算出的差異如Datediff的月,日或小時,但問題是如何將其轉換爲年,月和日期。這實際上很複雜,因爲我們不知道兩個月之間會有多少天(30,31天或28天)。

我使用這個經典的ASP代碼來轉換差異,但有很多缺點。

Function Convert_Date_to_Text(tarih1,tarih2,useDates) 

if (tarih1<>"" AND tarih2<>"") then 
    if Tarih_Araligi_Belirle(tarih1,tarih2,"day")>0 then 

     Date1_Year   = Year(tarih1) 
     Date1_Month   = Month(tarih1) 
     Date1_Day   = Day(tarih1) 
     Date2_Year   = Year(tarih2) 
     Date2_Month   = Month(tarih2) 
     Date2_Day   = Day(tarih2) 

     If (Date1_Month = 12) and (Date1_Day = 31) and 
        (Date2_Month = 1) and (Date2_Day = 1) Then 
      NoOfyears  = Date2_Year - Date1_Year - 1 
      NoOfmonths  = 0 
      NoOfdays  = 1 
     Else 
      NoOfyears  = Date2_Year - Date1_Year 
      NoOfmonths  = Date2_Month - Date1_Month 
      NoOfdays  = Date2_Day - Date1_Day 
     End If 

     If NoOfyears = 1 Then 
      FormatString  = "1 year " 
     Else If NoOfyears <= 0 then 
      FormatString  = "" 
     Else 
      FormatString  = CStr(NoOfyears) & " years " 
     End If:End If 

     If NoOfmonths = 1 Then 
      FormatString  = FormatString & "1 month" 
     Else If NoOfmonths <= 0 then 
      FormatString  = FormatString 
     Else 
      FormatString  = FormatString & CStr(NoOfmonths) & " months " 
     End If:End If 

     if useDates=1 then 
      If NoOfdays = 1 Then 
       FormatString  = FormatString & "1 day" 
      Else If NoOfdays <= 0 Then 
       FormatString  = FormatString 
      Else  
       FormatString  = FormatString & CStr(NoOfdays) & " days" 
      End If:End If 
     end if 

    end if 
end if 

Convert_Date_to_Text  = FormatString 

    End Function 

本網站完全計算差異。 TimeAndDate.Com

注:我使用傳統的ASP有幾個原因(公司限制)。對不起,但我需要一個ASP功能。它看起來像在時間跨度ASP :(不存在

親切的問候

+1

如果它是傳統的ASP,不會添加'asp.net'標記 - 它們是相當不同的野獸。這就是爲什麼你使用TimeSpan獲得所有答案的原因...... – slugster 2011-04-27 02:43:04

+0

@slugster:感謝您的警告。我編輯了標籤。 – 2011-04-27 02:45:39

+0

經典的ASP日期時間函數可以在這裏找到http://www.w3schools.com/vbScript/vbscript_ref_functions.asp#date – Dee 2011-04-27 17:20:26

回答

1

這是我在過去使用的功能。如果你測試它,我想你會發現它是準確的。 Here's where I got it from.

Function YearsMonthsDays(Date1 As Date, Date2 As Date, Optional ShowAll As _ 
    Boolean = False, Optional Grammar As Boolean = True) 

    ' This function returns a string "X years, Y months, Z days" showing the time 
    ' between two dates. This function may be used in any VBA or VB project 

    ' Date1 and Date2 must either be dates, or strings that can be implicitly 
    ' converted to dates. If these arguments have time portions, the time portions 
    ' are ignored. If Date1 > Date2 (after ignoring time portions), the function 
    ' returns an empty string 

    ' ShowAll indicates whether all portions of the string "X years, Y months, Z days" 
    ' are included in the output. If ShowAll = True, all portions of the string are 
    ' always included. If ShowAll = False, then if the year portion is zero the year 
    ' part of the string is omitted, and if the year portion and month portion are both 
    ' zero, than both year and month portions are omitted. The day portion is always 
    ' included, and if at least one year has passed then the month portion is always 
    ' included 

    ' Grammar indicates whether to test years/months/days for singular or plural 

    ' By definition, a "full month" means that the day number in Date2 is >= the day 
    ' number in Date1, or Date1 and Date2 occur on the last days of their respective 
    ' months. A "full year" means that 12 "full months" have passed. 

    ' In Excel, this function is an alternative to the little-known DATEDIF. DATEDIF 
    ' usually works well, but can create strange results when a date is at month end. 
    ' Thus, this formula: 

    '  =DATEDIF(A1,B1,"y") & " years, " & DATEDIF(A1,B1,"ym") & " months, " & 
    '  DATEDIF(A1,B1,"md") & " days" 

    ' will return "0 years, 1 months, -2 days" for 31-Jan-2006 and 1-Mar-2006. 
    ' This function will return "0 years, 1 month, 1 day" 

    Dim TestYear As Long, TestMonth As Long, TestDay As Long 
    Dim TargetDate As Date, Last1 As Date, Last2 As Date 

    ' Strip time portions 
    Date1 = Int(Date1) 
    Date2 = Int(Date2) 

    ' Test for invalid dates 
    If Date1 > Date2 Then 
     YearsMonthsDays = "" 
     Exit Function 
    End If 

    ' Test for whether the calendar year is the same 
    If Year(Date2) > Year(Date1) Then 

     ' Different calendar year. 

     ' Test to see if calendar month is the same. If it is, we have to look at the 
     ' day to see if a full year has passed 
     If Month(Date2) = Month(Date1) Then 
      If Day(Date2) >= Day(Date1) Then 
       TestYear = DateDiff("yyyy", Date1, Date2) 
      Else 
       TestYear = DateDiff("yyyy", Date1, Date2) - 1 
      End If 

      ' In this case, a full year has definitely passed 
     ElseIf Month(Date2) > Month(Date1) Then 
      TestYear = DateDiff("yyyy", Date1, Date2) 

      ' A full year has not passed 
     Else 
      TestYear = DateDiff("yyyy", Date1, Date2) - 1 
     End If 

     ' Calendar year is the same, so a full year has not passed 
    Else 
     TestYear = 0 
    End If 

    ' Test to see how many full months have passed, in excess of the number of full 
    ' years 
    TestMonth = (DateDiff("m", DateSerial(Year(Date1), Month(Date1), 1), _ 
    DateSerial(Year(Date2), Month(Date2), 1)) + IIf(Day(Date2) >= _ 
    Day(Date1), 0, -1)) Mod 12 

    ' See how many days have passed, in excess of the number of full months. If the day 
    ' number for Date2 is >= that for Date1, it's simple 
    If Day(Date2) >= Day(Date1) Then 
     TestDay = Day(Date2) - Day(Date1) 

     ' If not, we have to test for end of the month 
    Else 
     Last1 = DateSerial(Year(Date2), Month(Date2), 0) 
     Last2 = DateSerial(Year(Date2), Month(Date2) + 1, 0) 
     TargetDate = DateSerial(Year(Date2), Month(Date2) - 1, Day(Date1)) 
     If Last2 = Date2 Then 
      If TestMonth = 11 Then 
       TestMonth = 0 
       TestYear = TestYear + 1 
      Else 
       TestMonth = TestMonth + 1 
      End If 
     Else 
      TestDay = DateDiff("d", IIf(TargetDate > Last1, Last1, TargetDate), Date2) 
     End If 
    End If 

    If ShowAll Or TestYear >= 1 Then 
     YearsMonthsDays = TestYear & IIf(TestYear = 1 And Grammar, " year, ", _ 
     " years, ") & TestMonth & IIf(TestMonth = 1 And Grammar, " month, ", _ 
     " months, ") & TestDay & IIf(TestDay = 1 And Grammar, " day", " days") 
    Else 
     If TestMonth >= 1 Then 
      YearsMonthsDays = TestMonth & IIf(TestMonth = 1 And Grammar, " month, ", _ 
      " months, ") & TestDay & IIf(TestDay = 1 And Grammar, " day", " days") 
     Else 
      YearsMonthsDays = TestDay & IIf(TestDay = 1 And Grammar, " day", " days") 
     End If 
    End If 

End Function 
2

如果你可以將輸入字符串轉換爲DateTime變量,你可以嘗試這樣的事:

DateTime starTime = //something; 
DateTime endTime = //something; 
TimeSpan oneDay = new TimeSpan(1, 0, 0, 0); //creates a timespan of 1 day 
TimeSpan deltaTime = (endTime - startTime) - oneDay; 

我會asume ASP有DateTimeTimeSpan變量類型

+0

我做了一個快速搜索,但我認爲ASP沒有像TimeSpan這樣的內置類。這是我發現的功能:[ASPFAQ](http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-convert-a-timespan-in-seconds-to-hh-mm -ss.html),但它不會將其轉換爲數月或數年。 – 2011-04-27 02:30:54

+0

你有'DateTime'嗎? – ohmusama 2011-04-27 03:01:21

+0

不經典ASP – Dee 2011-04-27 17:18:32

0

您可以減去DateTime對象,以得到一個TimeSpan對象:

DateTime startDate = GetStartDate(); 
DateTime endDate = GetEndDate(); 
TimeSpan duration = endDate - startDate; 
+0

您的答案是正確的ASP.NET,但OP已修改他們的問題,以限制到經典的ASP .... – slugster 2011-04-27 02:46:53

+0

不經典的ASP – Dee 2011-04-27 17:18:06

1

這個怎麼樣? (無時間跨度,但不知道,如果傳統的ASP兼容)

DateTime dateTime1 = new DateTime(2003,2,2); 
DateTime dateTime2 = new DateTime(2001,1,1); 

int daysDiff = dateTime1.Day - dateTime2.Day; 
int monthsDiff = dateTime1.Month - dateTime2.Month; 
int yearsDiff = dateTime1.Year - dateTime2.Year; 

if (daysDiff < 0) 
{ 
    daysDiff += DateTime.DaysInMonth(dateTime1.Year, dateTime1.Month); 
    monthsDiff--; 
} 

if (monthsDiff < 0) 
{ 
    monthsDiff += 12; 
    yearsDiff--; 
} 

Console.WriteLine(daysDiff); 
Console.WriteLine(monthsDiff); 
Console.WriteLine(yearsDiff); 
+0

謝謝你的答案,但ASP不兼容我想和抱歉,我需要一個ASP功能。 – 2011-04-27 02:39:42

+0

不是經典的asp – Dee 2011-04-27 17:17:49

0

article包括則DateDiff類:

// ---------------------------------------------------------------------- 
public void DateDiffSample() 
{ 
    DateTime date1 = new DateTime(2009, 11, 8, 7, 13, 59); 
    Console.WriteLine("Date1: {0}", date1); 
    // > Date1: 08.11.2009 07:13:59 
    DateTime date2 = new DateTime(2011, 3, 20, 19, 55, 28); 
    Console.WriteLine("Date2: {0}", date2); 
    // > Date2: 20.03.2011 19:55:28 

    DateDiff dateDiff = new DateDiff(date1, date2); 

    // differences 
    Console.WriteLine("DateDiff.Years: {0}", dateDiff.Years); 
    // > DateDiff.Years: 1 
    Console.WriteLine("DateDiff.Quarters: {0}", dateDiff.Quarters); 
    // > DateDiff.Quarters: 5 
    Console.WriteLine("DateDiff.Months: {0}", dateDiff.Months); 
    // > DateDiff.Months: 16 
    Console.WriteLine("DateDiff.Weeks: {0}", dateDiff.Weeks); 
    // > DateDiff.Weeks: 70 
    Console.WriteLine("DateDiff.Days: {0}", dateDiff.Days); 
    // > DateDiff.Days: 497 
    Console.WriteLine("DateDiff.Weekdays: {0}", dateDiff.Weekdays); 
    // > DateDiff.Weekdays: 71 
    Console.WriteLine("DateDiff.Hours: {0}", dateDiff.Hours); 
    // > DateDiff.Hours: 11940 
    Console.WriteLine("DateDiff.Minutes: {0}", dateDiff.Minutes); 
    // > DateDiff.Minutes: 716441 
    Console.WriteLine("DateDiff.Seconds: {0}", dateDiff.Seconds); 
    // > DateDiff.Seconds: 42986489 

    // elapsed 
    Console.WriteLine("DateDiff.ElapsedYears: {0}", dateDiff.ElapsedYears); 
    // > DateDiff.ElapsedYears: 1 
    Console.WriteLine("DateDiff.ElapsedMonths: {0}", dateDiff.ElapsedMonths); 
    // > DateDiff.ElapsedMonths: 4 
    Console.WriteLine("DateDiff.ElapsedDays: {0}", dateDiff.ElapsedDays); 
    // > DateDiff.ElapsedDays: 12 
    Console.WriteLine("DateDiff.ElapsedHours: {0}", dateDiff.ElapsedHours); 
    // > DateDiff.ElapsedHours: 12 
    Console.WriteLine("DateDiff.ElapsedMinutes: {0}", dateDiff.ElapsedMinutes); 
    // > DateDiff.ElapsedMinutes: 41 
    Console.WriteLine("DateDiff.ElapsedSeconds: {0}", dateDiff.ElapsedSeconds); 
    // > DateDiff.ElapsedSeconds: 29 

    // description 
    Console.WriteLine("DateDiff.GetDescription(1): {0}", dateDiff.GetDescription(1)); 
    // > DateDiff.GetDescription(1): 1 Year 
    Console.WriteLine("DateDiff.GetDescription(2): {0}", dateDiff.GetDescription(2)); 
    // > DateDiff.GetDescription(2): 1 Year 4 Months 
    Console.WriteLine("DateDiff.GetDescription(3): {0}", dateDiff.GetDescription(3)); 
    // > DateDiff.GetDescription(3): 1 Year 4 Months 12 Days 
    Console.WriteLine("DateDiff.GetDescription(4): {0}", dateDiff.GetDescription(4)); 
    // > DateDiff.GetDescription(4): 1 Year 4 Months 12 Days 12 Hours 
    Console.WriteLine("DateDiff.GetDescription(5): {0}", dateDiff.GetDescription(5)); 
    // > DateDiff.GetDescription(5): 1 Year 4 Months 12 Days 12 Hours 41 Mins 
    Console.WriteLine("DateDiff.GetDescription(6): {0}", dateDiff.GetDescription(6)); 
    // > DateDiff.GetDescription(6): 1 Year 4 Months 12 Days 12 Hours 41 Mins 29 Secs 
} // DateDiffSample 
0

Dim intYears 
Dim intMonths 
Dim intDays 

Dim strDate1 
Dim strDate2 

Dim strAnswer 

strDate1 = "01/26/2010" 
strDate2 = "02/15/2010" 

intYears = DateDiff("yyyy",strDate1,strDate2) 
intMonths = DateDiff("m",strDate1,strDate2) 
intDays = DateDiff("d",strDate1,strDate2) 

strAnswer = "" 
if intYears > 0 then 
    strAnswer = strAnswer & CStr(intYears) & "years " 
end if 
if intMonths > 0 then 
    strAnswer = strAnswer & CStr(intMonths) & "months" 
end if 
if intDays > 0 then 
    strAnswer = strAnswer & CStr(intDays) & "days" 
end if 

Response.Write("The difference between these two dates is " & strAnswer)