0

好的,所以我有以下代码来获取文件夹名称中的最后 4 个字符,它始终是日期,格式为 MMDD。该日期也将始终是星期一。

下面修剪的文件夹名称示例:C:\Temp\Textfiles\20120813

Dim MMDD_Date As String = Microsoft.VisualBasic.Right(TextBox1_Path.Text, 4)

我不确定的是如何计算那一周的剩余天数,以星期五结束。很明显,您不能将字符串转换为 int 并添加 1,因为某些月份有 29、30 或 31,具体取决于闰年。

最终结果将是这样的:

周一:0813

周二:0814

周三:0815

周四:0816

周五:0817

有什么建议可以为我指明正确的方向吗?

4

3 回答 3

1

使用年、月和日创建 DateTime。使用该创建日期轻松获取后续日期。

   ' Parse Y, M and D (Integers) from folder name
   ' ...

    Dim X As DateTime
    X = New DateTime(Y, M, D) 

    Dim Tue As String = X.AddDays(1).ToString("MMDD")
    Dim Wen As String = X.AddDays(2).ToString("MMDD")
    Dim Thr As String = X.AddDays(3).ToString("MMDD")
    Dim Fri As String = X.AddDays(4).ToString("MMDD")
于 2012-08-14T13:43:31.117 回答
1

看起来你想要这样的东西。我不完全确定。这是在 C# 中,我假设您可以将其转换为 VB.NET。

var folderName = @"C:\Temp\Textfiles\20120813"; //Test input
var dateString = folderName.Substring(folderName.Length - 8);
var date = DateTime.ParseExact(dateString, "yyyyMMdd", CultureInfo.InvariantCulture);

var counter = date.DayOfWeek; //OP says this is always a Monday, but is not hardcoded
var mmdd = int.Parse(folderName.Substring(folderName.Length - 4));
var myDates = new List<string>();

do
{
  myDates.Add(string.Format("{0}: {1:0000}", counter.ToString().Substring(0, 3), mmdd++)); //E.g. "Mon: 0813"
} while (counter++ < DayOfWeek.Friday);
于 2012-08-14T14:28:49.650 回答
0

好吧,这就是我想出的。听取了@channs 和@Dusan 的建议,并结合了这些努力。

    Dim FolderDate As String = TextBox1_Path.Text
    Dim DateString = FolderDate.Substring(FolderDate.Length - 8)
    Dim d = DateTime.ParseExact(DateString, "yyyyMMdd", CultureInfo.InvariantCulture)

    Dim Mon As String = d.ToString("MMdd")
    Dim Tue As String = d.AddDays(1).ToString("MMdd")
    Dim Wed As String = d.AddDays(2).ToString("MMdd")
    Dim Thu As String = d.AddDays(3).ToString("MMdd")
    Dim Fri As String = d.AddDays(4).ToString("MMdd")

    'test output
    MsgBox(Mon)
    MsgBox(Tue)
    MsgBox(Wed)
    MsgBox(Thu)
    MsgBox(Fri)
于 2012-08-14T16:38:59.333 回答