vb.net 从日期减去天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23497179/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:30:17  来源:igfitidea点击:

Subtracting days from date

vb.netdatevisual-studio-2012

提问by elmonko

I'm struggling in vein to work out how to remove 5 days from today's date...

我正在努力研究如何从今天的日期中删除 5 天......

I have the following simple code that searches compares the result of a text file array search and then compares them to today's date. If the date within the text file is older than today then it deletes, if not it doesn't.

我有以下简单的代码搜索比较文本文件数组搜索的结果,然后将它们与今天的日期进行比较。如果文本文件中的日期早于今天,则删除,否则不删除。

What i want though is to say if the date in the text file is 5 days or older then delete.

我想说的是,如果文本文件中的日期是 5 天或更早,则删除。

This is being used in the English date format.

这是在英文日期格式中使用的。

    Sub KillSuccess()
    Dim enUK As New CultureInfo("en-GB")

    Dim killdate As String = DateTime.Now.ToString("d", enUK)

    For Me.lo = 0 To UBound(textcis)
        If textcis(lo).oDte < killdate Then
            File.Delete(textcis(lo).oPath & ".txt")
        End If
    Next

End Sub 

Thanks

谢谢

回答by Simon Martin

You can use the AddDaysmethod; in code that would be something like this:

您可以使用该AddDays方法;在代码中,将是这样的:

Dim today = DateTime.Now
Dim answer = today.AddDays(-5)

msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx

msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx

Which would make your code

这将使您的代码

Sub KillSuccess()

    Dim killdate = DateTime.Now.AddDays(-5)

    For Me.lo = 0 To UBound(textcis)
        If textcis(lo).oDte < killdate Then
            File.Delete(textcis(lo).oPath & ".txt")
        End If
    Next
End Sub