实际上,以下是您执行此操作最准确的方法,因为“ 1个月”的定义会根据它所在的月份而变化,并且其他答案都没有考虑到这一点!如果您想了解框架中未包含的有关该问题的更多信息,可以阅读这篇文章:具有.Years和.Months的Real Timespan对象(但是,阅读和理解下面的函数不是必需的,它可以正常工作100%,而不会出现其他人喜欢使用的近似值固有的不准确性-并可以用您的框架中内置的.Reverse函数随意替换.ReverseIt函数(此处仅出于完整性)。
请注意,您可以获得任意数量的日期/时间精度,秒和分钟或秒,分钟和天,最多可达几年(可能包含6个部分/段)。如果您指定前两个且已超过一年,则它将返回“ 1年零3个月前”,而不会返回其余部分,因为您已请求了两个细分。如果仅使用了几个小时,则只会返回“ 2小时零一分钟”。当然,如果您指定1、2、3、4、5或6个segmet,则同样的规则适用(由于秒,分钟,小时,天,月,年,年仅产生6种类型,因此最大为6)。它还将纠正语法问题,例如“分钟”与“分钟”,具体取决于它是否为1分钟或更长,所有类型均相同,并且生成的“字符串”将始终在语法上正确。
以下是一些使用示例:bAllowSegments标识要显示的段数...即:如果为3,则返回字符串为(作为示例)... "3 years, 2 months and 13 days"
(不包括小时,分钟和秒作为前3个时间)类别)),但是,如果日期是较新的日期(例如几天前的日期),则指定相同的细分(3)将返回"4 days, 1 hour and 13 minutes ago"
,因此将所有内容都考虑在内!
如果bAllowSegments为2,它将返回"3 years and 2 months"
,如果返回6(最大值)"3 years, 2 months, 13 days, 13 hours, 29 minutes and 9 seconds"
,则应注意,这将NEVER RETURN
是类似的事情,"0 years, 0 months, 0 days, 3 hours, 2 minutes and 13 seconds ago"
因为即使您指定了6个段,它也可以理解前3个段中没有日期数据并忽略它们。 ,所以请放心:)。当然,如果其中有一个段为0,则在形成字符串时会考虑到这一点,并显示为,"3 days and 4 seconds ago"
而忽略“ 0 hours”部分!享受,如果您愿意,请发表评论。
Public Function RealTimeUntilNow(ByVal dt As DateTime, Optional ByVal bAllowSegments As Byte = 2) As String
' bAllowSegments identifies how many segments to show... ie: if 3, then return string would be (as an example)...
' "3 years, 2 months and 13 days" the top 3 time categories are returned, if bAllowSegments is 2 it would return
' "3 years and 2 months" and if 6 (maximum value) would return "3 years, 2 months, 13 days, 13 hours, 29 minutes and 9 seconds"
Dim rYears, rMonths, rDays, rHours, rMinutes, rSeconds As Int16
Dim dtNow = DateTime.Now
Dim daysInBaseMonth = Date.DaysInMonth(dt.Year, dt.Month)
rYears = dtNow.Year - dt.Year
rMonths = dtNow.Month - dt.Month
If rMonths < 0 Then rMonths += 12 : rYears -= 1 ' add 1 year to months, and remove 1 year from years.
rDays = dtNow.Day - dt.Day
If rDays < 0 Then rDays += daysInBaseMonth : rMonths -= 1
rHours = dtNow.Hour - dt.Hour
If rHours < 0 Then rHours += 24 : rDays -= 1
rMinutes = dtNow.Minute - dt.Minute
If rMinutes < 0 Then rMinutes += 60 : rHours -= 1
rSeconds = dtNow.Second - dt.Second
If rSeconds < 0 Then rSeconds += 60 : rMinutes -= 1
' this is the display functionality
Dim sb As StringBuilder = New StringBuilder()
Dim iSegmentsAdded As Int16 = 0
If rYears > 0 Then sb.Append(rYears) : sb.Append(" year" & If(rYears <> 1, "s", "") & ", ") : iSegmentsAdded += 1
If bAllowSegments = iSegmentsAdded Then GoTo parseAndReturn
If rMonths > 0 Then sb.AppendFormat(rMonths) : sb.Append(" month" & If(rMonths <> 1, "s", "") & ", ") : iSegmentsAdded += 1
If bAllowSegments = iSegmentsAdded Then GoTo parseAndReturn
If rDays > 0 Then sb.Append(rDays) : sb.Append(" day" & If(rDays <> 1, "s", "") & ", ") : iSegmentsAdded += 1
If bAllowSegments = iSegmentsAdded Then GoTo parseAndReturn
If rHours > 0 Then sb.Append(rHours) : sb.Append(" hour" & If(rHours <> 1, "s", "") & ", ") : iSegmentsAdded += 1
If bAllowSegments = iSegmentsAdded Then GoTo parseAndReturn
If rMinutes > 0 Then sb.Append(rMinutes) : sb.Append(" minute" & If(rMinutes <> 1, "s", "") & ", ") : iSegmentsAdded += 1
If bAllowSegments = iSegmentsAdded Then GoTo parseAndReturn
If rSeconds > 0 Then sb.Append(rSeconds) : sb.Append(" second" & If(rSeconds <> 1, "s", "") & "") : iSegmentsAdded += 1
parseAndReturn:
' if the string is entirely empty, that means it was just posted so its less than a second ago, and an empty string getting passed will cause an error
' so we construct our own meaningful string which will still fit into the "Posted * ago " syntax...
If sb.ToString = "" Then sb.Append("less than 1 second")
Return ReplaceLast(sb.ToString.TrimEnd(" ", ",").ToString, ",", " and")
End Function
当然,您将需要一个“ ReplaceLast”函数,该函数需要一个源字符串,一个参数指定需要替换的内容,另一个arg指定要替换的内容,并且仅替换该字符串的最后一次出现...如果您没有它或不想实现它,我已将其包括在内,因此在这里,它可以“按原样”运行而无需修改。我知道不再需要反向功能(存在于.net中),但是ReplaceLast和ReverseIt函数是从.net之前的日期开始继承的,因此请原谅它看起来过旧(仍然可以100%使用, em已有十多年的历史了,可以保证它们没有错误)... :)。干杯。
<Extension()> _
Public Function ReplaceLast(ByVal sReplacable As String, ByVal sReplaceWhat As String, ByVal sReplaceWith As String) As String
' let empty string arguments run, incase we dont know if we are sending and empty string or not.
sReplacable = sReplacable.ReverseIt
sReplacable = Replace(sReplacable, sReplaceWhat.ReverseIt, sReplaceWith.ReverseIt, , 1) ' only does first item on reversed version!
Return sReplacable.ReverseIt.ToString
End Function
<Extension()> _
Public Function ReverseIt(ByVal strS As String, Optional ByVal n As Integer = -1) As String
Dim strTempX As String = "", intI As Integer
If n > strS.Length Or n = -1 Then n = strS.Length
For intI = n To 1 Step -1
strTempX = strTempX + Mid(strS, intI, 1)
Next intI
ReverseIt = strTempX + Right(strS, Len(strS) - n)
End Function