vb.net VB 2010:如何计算日期差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6190694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB 2010: How to calculate a date difference?
提问by Florian Müller
I'd like do make a program which returns you how old you are, in years, months, weeks and days. But I didn't get it to compare different times.
我想做一个程序,以年、月、周和天为单位返回您的年龄。但我没有得到它来比较不同的时间。
Input is a string which looks like 01.01.2011 (dd.mm.yyyy).
输入是一个看起来像 01.01.2011 (dd.mm.yyyy) 的字符串。
Please, can somebody help?
拜托,有人可以帮忙吗?
Thanks very much!
非常感谢!
EDIT:
编辑:
My code so far is this:
到目前为止,我的代码是这样的:
Try
dim date1 as string = '01.01.2011'
' Today
Dim date2 As Date
date2 = Date.Now
' number of seconds since date1
Dim seconds As Long
seconds = DateDiff("s", date1, date2)
lbl_seconds.Text = seconds.ToString & " Seconds"
' Number of minutes since date1
Dim Minutes As Long
Minutes = DateDiff("n", date1, date2)
lbl_minutes.Text = Minutes.ToString & " Minutes"
' Number of hours since date1
Dim hours As Long
hours = DateDiff("h", date1, date2)
lbl_hours.Text = hours.ToString & " Hours"
' Days
Dim days As Long
days = DateDiff("d", date1, date2)
lbl_days.Text = days.ToString & " Days"
' weeks
Dim weeks As Long
weeks = DateDiff("ww", date1, date2)
lbl_weeks.Text = weeks.ToString & " Weeks"
'months
Dim months As Long
months = DateDiff("m", date1, date2)
lbl_months.Text = months.ToString & " Months"
' Years
Dim years As Long
years = DateDiff("yyyy", date1, date2)
lbl_years.Text = years.ToString & " Years"
Catch ex As Exception
date1 = "01.01.2011"
MsgBox("not a valid date given!")
End Try
回答by Thomas Li
DateDiff is a VB function and is not part of the standard .Net library (So C# can't use it).
DateDiff 是一个 VB 函数,不是标准 .Net 库的一部分(所以 C# 不能使用它)。
It's easier to use the TimeSpanclass and the toString() method with Custom TimeSpan Format Stringto get what you want.
将TimeSpan类和toString() 方法与自定义 TimeSpan 格式字符串一起使用更容易获得您想要的。
Edit:
编辑:
Here's the code, you can compare result to http://www.easycalculation.com/date-day/age-calculator.php:
这是代码,您可以将结果与http://www.easycalculation.com/date-day/age-calculator.php进行比较:
Dim birthday As New DateTime(1990, 1, 1)
Dim ts As TimeSpan = DateTime.Now.Subtract(birthday)
Dim years As Integer, months As Integer, days As Integer, hours As Integer, minutes As Integer, seconds As Integer
' compute difference in total months
months = 12 * (DateTime.Now.Year - birthday.Year) + (DateTime.Now.Month - birthday.Month)
' based upon the 'days',
' adjust months & compute actual days difference
If DateTime.Now.Day < birthday.Day Then
months -= 1
days = DateTime.DaysInMonth(birthday.Year, birthday.Month) - birthday.Day + DateTime.Now.Day
Else
days = DateTime.Now.Day - birthday.Day
End If
' compute years & actual months
years = Math.Floor(months / 12)
months -= years * 12
hours = ts.Hours
minutes = ts.Minutes
seconds = ts.Seconds