[英]convert number of days into years,months,days
我需要將天數轉換為年,月,日。
示例:根據他的join(DOJ)
日期join(DOJ)
和Relieve(DOR)
日期Relieve(DOR)
計算的員工經驗。 我們有一個DOJ, DOR and Number
他工作的天數。
必須計算多少年,幾月幾日。
Example : DOJ = 14 Feb 2000
DOR = 08 aug 2013
Output : 13 Years - 5 Months - 25 Days
提前致謝....
這對我有用:
var dor = new DateTime(2013, 08, 08);
var doj = new DateTime(2000, 02, 14);
var totalmonths = (dor.Year - doj.Year) * 12 + dor.Month - doj.Month;
totalmonths += dor.Day < doj.Day ? -1 : 0;
var years = totalmonths / 12;
var months = totalmonths % 12;
var days = dor.Subtract(doj.AddMonths(totalmonths)).Days;
您無法將天數轉換成年月日,因為您不知道這些日子在哪里。 例如,它們可能跨越Feb年2月29日。
但是,您已經有兩個日期可以使用,因此您可以像這樣計算此值:
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
Dim doj As Date = New Date(2000, 2, 14)
Dim dor As Date = New Date(2013, 8, 8)
MessageBox.Show(GetDateSpanText(doj, dor))
End Sub
Public Shared Function GetDateSpanText(fromDate As DateTime, Optional toDate As DateTime = Nothing) As String
Try
Dim years As Integer = 0, months As Integer = 0, days As Integer = 0
If toDate = Nothing Then toDate = DateTime.Now
Do Until toDate.AddYears(-1) < fromDate
years += 1
toDate = toDate.AddYears(-1)
Loop
Do Until toDate.AddMonths(-1) < fromDate
months += 1
toDate = toDate.AddMonths(-1)
Loop
Do Until toDate.AddDays(-1) < fromDate
days += 1
toDate = toDate.AddDays(-1)
Loop
Return String.Format("{0} Years {1} Months {2} Days", years, months, days)
Catch ex As Exception
Return "Error"
End Try
End Function
您可以使用月份中的年和月計算,如果您有30個月,則使用以下代碼將返回2年和6個月。
Math.Round((double)Months / 12,0)年和月%12月
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.