0

Alright, so I have the following code to grab the last 4 characters in a folder name, which will always be date, formatted as MMDD. This date will always be a Monday date as well.

Folder name example being trimmed below: C:\Temp\Textfiles\20120813

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

What I'm unsure of, is how to calculate the remaining days for that week, ending on a Friday. Obvioulsy you cant convert the string to an int and add 1 since some months have 29, 30 or 31 depending on leap years.

The end result would be something like this:

Mon: 0813

Tue: 0814

Wed: 0815

Thu: 0816

Fri: 0817

Any suggestions to point me in the right direction?

4
  • Do you have the 'year' (YYYY) component?
    – Channs
    Aug 14, 2012 at 13:36
  • Yes, the year component is part of the folder name. Folder name example: C:\Temp\Textfiles\20120813
    – Muhnamana
    Aug 14, 2012 at 13:42
  • Is this your case? Please help me understand. If the folder name ends with '0814' (Tue), you want a list of strings from '0814' (Tue) to '0817' (Fri). If the folder name ends with '0817' (Fri), you want just that. If the folder name is Sat or Sun, its a no-op.
    – Channs
    Aug 14, 2012 at 14:14
  • No, the folder will always end in the Monday date of each week. I need to calculate the rest of the week and only return MMDD.
    – Muhnamana
    Aug 14, 2012 at 14:15

3 Answers 3

1

Looks like you want something like this. I am not entirely sure. This is in C#, I assume you can convert it to 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
  • Yeah I'll give it a try, not familiar with c# though. Thanks.
    – Muhnamana
    Aug 14, 2012 at 16:20
  • I think I figured it out. Took some of your code and the other posters code and combined? Well it seems to work.
    – Muhnamana
    Aug 14, 2012 at 16:38
1

Create DateTime using year, month and day. Use that created date to easily get subsequent days.

   ' 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")
0
0

Alright, well here's what I came up with. Took suggestions from @channs and @Dusan and combined the efforts.

    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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.