好吧,所以我有以下代码来获取文件夹名称中的最后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
有任何建议指出我正确的方向吗?
答案 0 :(得分: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")
答案 1 :(得分: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);
答案 2 :(得分: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)