VB.Net 2005,如何从(当前日期减去 7 天)中减去一个日期并生成天数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3810872/
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.Net 2005, how can I subtract a date from the (current date minus 7 days) and produce a number of days?
提问by Jules
I have a date in the future e.g. 13/10/2008 I need to subtract the current date (today is the 28/09/2010) minus 7 days, so thats 21/09/2010 minus 13/10/2008, which would equal erm, 720 something ?
我有一个未来的日期,例如 13/10/2008 我需要减去当前日期(今天是 28/09/2010)减去 7 天,所以那就是 21/09/2010 减去 13/10/2008,这将等于 erm,720 什么?
But the current date won't always be 28/09/2010, obviously.
但显然,当前日期并不总是 28/09/2010。
I need the code for this.
我需要这个代码。
EDIT: When i said future I mean past :)
编辑:当我说未来时,我的意思是过去:)
回答by Darin Dimitrov
Sub Main()
Dim dt As DateTime = New DateTime(2008, 10, 13)
' be careful what you are subtracting from what
' the date you have is not in the future (year 2008)
' if the date is in the future: (dt.Subtract(DateTime.Now.AddDays(-7))).TotalDays
' or simply take the absolute value
Dim days As Double = (DateTime.Now.AddDays(-7).Subtract(dt)).TotalDays
Console.WriteLine(days)
End Sub
You will also notice that the TotalDaysproperty is of type Double.
回答by Ishwar Singh
Dim ValidDate As Date =cDate("Tuesday, December 31, 2013") 'A date in Future
Dim date1 As New System.DateTime(ValidDate.Year, ValidDate.Month, ValidDate.Day)
Dim date2 = Now
Dim Diff1 As System.TimeSpan
Diff1 = date1.Subtract(date2)
Dim TotRemDays = (Int(Diff1.TotalDays))
MsgBox(TotRemDays)
回答by veggerby
13/10/2008 is not exactly in the future :)
2008 年 13 月 10 日不完全是未来 :)
Sorry for using C# code, but:
很抱歉使用 C# 代码,但是:
(dateInFuture - DateTime.Now.AddDays(-7)).TotalDays
Should work. Of course the other way around if you mean in the past:
应该管用。当然,如果您的意思是过去,则相反:
(DateTime.Now.AddDays(-7) - dateInPast).TotalDays
回答by Matteo Italia
"I need the code for this" seems a bit too much like "Plz give meh teh codez", and your "date in the future" seems a little bit in the past.
“我需要这个代码”似乎有点像“请给我代码”,而你的“未来约会”似乎有点过去了。
Anyway, you should investigate the relevant methods of the DateTime
structure, in particular the Subtractmethod (both overloads, or in alternative its subtraction operator), and you should have a look at the TimeSpan
structure too.
无论如何,您应该研究DateTime
结构的相关方法,特别是Subtract方法(两个重载,或者它的减法运算符),并且您也应该查看TimeSpan
结构。
You could create a DateTime
for the date of today, subtract a TimeSpan
of 7 days to it, and then subtract such result to a DateTime
representing your date in the future (or, if it is in the past, do the opposite). You'll get a TimeSpan
representing the difference in time between the two dates, from which you can easily get the number of days using its Days
property.
您可以DateTime
为今天的日期创建 a ,减去TimeSpan
7 天的a ,然后将这样的结果减去DateTime
代表您未来日期的a (或者,如果是过去,则执行相反的操作)。您将获得一个TimeSpan
代表两个日期之间的时间差的值,您可以从中轻松获得使用其Days
属性的天数。
As other said, to do the first subtraction you can also use the AddDays
method of the DateTime
structure.
正如其他人所说,要进行第一次减法,您也可以使用结构的AddDays
方法DateTime
。