☆ 有些事現在不做,就一輩子都不會做了 ☆

最新回應

若要計算二個日期經過幾月又幾天,需要考慮期間每個月份的天數,每個月的天數是不一樣的。
所以我們可以先計算經過幾個月,然後把起始日期累加異差月份數,再計算差異天數。

        Dim oDate1 As Date = Date.Parse("2008/5/1")
        Dim oDate2 As Date = Date.Parse("2008/7/10")

        '先算月份
        Dim iMonths As Integer = DateDiff(DateInterval.Month, oDate1, oDate2)
        '再算天數
        Dim iDays As Integer = DateDiff(DateInterval.Day, DateAdd(DateInterval.Month, iMonths, oDate1), oDate2)

        MsgBox(String.Format("經過{0}月{1}天", iMonths, iDays))

回應

  • linco 2008/7/22 上午 09:46 回覆

    # re: 計算二個日期經過幾月又幾天

    請問一下MsgBox(String.Format("經過{0}月{1}天", iMonths, iDays))
    其中{0}跟{1}是啥?

  • dotjum 2008/7/22 上午 09:54 回覆

    # re: 計算二個日期經過幾月又幾天

    幫版主回答一下,
     String.Format 是對應後面順序的第一個,第二個變數
    String.Format 方法(System)

    以與對應物件值相等的文字,取代指定String 中的每個格式項目。
    msdn.microsoft.com/zh-tw/library/system.string.format(VS.80).aspx

  • fredli 2008/8/18 上午 09:34 回覆

    # re: 計算二個日期經過幾月又幾天

    在取得Month的那邊,會有一些問題
    譬如2008/10/10到2008/11/05
    Month = 1
    但實際上應該要=0才對

    以下為我所使用的算法(C#)
    PS:因為處理情況皆是endDate >= startDate,所以沒考慮到endDate < startDate的邏輯
    DateTime startDate = new DateTime(2008,10,10);
    DateTime endDate = new DateTime(2008,11,5);
    TimeSpan span = endDate - startDate;
    int iMonth = (int)(span.Days / 31);
    while (startDate.AddMonths(iMonth + 1) <= endDate)
    {
    iMonth++;
    }


*標 題:

*姓 名:

 電子郵件: (將不會被顯示)

 個人網頁:

*回應

登入後使用進階評論

Please add 5 and 3 and type the answer here: