datetime - 如何计算两个不同的日期,例如 2 年零 5 个月

标签 datetime asp-classic datediff

我想计算两个日期之间的差异,并想将其转换为 2 年、5 个月或仅 3 个月,或者考虑到所有月份都是 30 天,根据差异转换为 2 天...

例如;

自并包括: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 中似乎不存在 TimeSpan :(

亲切的问候

最佳答案

如果您可以将输入字符串转换为 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;

我假设 asp 具有 DateTimeTimeSpan 变量类型。

关于datetime - 如何计算两个不同的日期,例如 2 年零 5 个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5798626/

相关文章:

UTC和祖鲁时间的Java 8相等?

javascript - Onclick传递两个变量时的语法问题,经典asp

asp-classic - 在经典 ASP 中创建文件夹

asp-classic - 经典 ASP : I'm getting a type mismatch error when I shouldn't

mysql - 如何对开始/结束数据的总小时数/天进行分组(每天多个条目,一些跨越午夜)?

php - DateTime "first day of last month"第一天不返回

android - 如何在查询 sqlite 时格式化 DateTime 列?

python - 在python中将字符串转换为日期时间并与当前时间进行比较

python - python3中的日期时间到十进制小时和分钟

sql计算两次之间的两个日期的分钟数