2

Currently working on the conversion of the VB6 to C# but encounters problems regarding DateAdd & DatePart. Hope if anyone is good in VB6 could enlighten me on this. Thanks.

Below are the VB6 codes:

dFilterDate2 = DateAdd("d", -4, dFilterDate1)
dFilterDate3 = DateAdd("y", -(DatePart("y", dFilterDate1) - 1), dFilterDate1)
dFilterDate4 = DateAdd("yyyy", -1, dFilterDate3)
dFilterDate5 = DateAdd("d", Weekday(CDate(dFilterDate1)) - 1, CDate(DateAdd("ww", Format(CDate(dFilterDate1), "ww") - 1, CDate(CDate(dFilterDate4) - Weekday(CDate(dFilterDate4))))))

And below are my code in C#.

dFilterDate2 = dFilterDate1.AddDays(-4);
dFilterDate3 = dFilterDate1.AddYears(-dFilterDate1.DayOfYear-1);
dFilterDate4 = dFilterDate3.AddYears(-1);
dFilterDate5 = ?;
eglease
  • 2,445
  • 11
  • 18
  • 28
RogerSK
  • 393
  • 1
  • 18
  • Does this answer your question? [Equivalent of WeekDay Function of VB6 in C#](https://stackoverflow.com/questions/400421/equivalent-of-weekday-function-of-vb6-in-c-sharp) – GSerg Sep 22 '21 at 18:26
  • What is `dFilterDate1` and what is an answer in VB6 for `dFilterDate5` given some input so this can be tested? – eglease Sep 22 '21 at 21:34
  • @GSerg Thanks for provides the link, however it looks strange after combination with DateAdd & Weekday. – RogerSK Sep 23 '21 at 04:08
  • @eglease Thanks for the comment. `dFilterDate1` can be any date, but `dFilterDate5` looks complicated for me with the combination of the DateAdd & WeekDay hence I am unable to figure it out – RogerSK Sep 23 '21 at 04:10
  • What is the business rule? Do you know why it was written this way? – eglease Sep 23 '21 at 04:45
  • 1
    @eglease That's no such document for me to refer since the system has been used for at least 10 years(VB6) and it will be decommissioned soon. – RogerSK Sep 23 '21 at 07:53
  • I am going to try to solve this. May take a moment but I got it working in VB6. – eglease Sep 23 '21 at 13:42
  • @eglease Yep, it's the original source code from the old project, should work well but seem complicated for me and difficult to re-write it to other language – RogerSK Sep 23 '21 at 13:46
  • We are in the same boat. I have to rewrite/refactor/re-engineer multiple VB6 project into C# and/or Go and/or SQL. – eglease Sep 23 '21 at 13:53
  • 1
    The conversion for dFilterDate3 is incorrect. The original code will (simply?) produce a date of january 1 with the year that dFilterDate3 has. For instance, a date of 9/23/2021 will calculate to 1/1/2021. The converted code line will subtract from the year the number of days of the day of the year, less an additional one; a date of 9/23/2021 (day 266) will produce 9/23/1754. – MarkL Sep 23 '21 at 14:57

1 Answers1

3

This took me a while but this is what I go.

First, dFilterDate3 is not correct. It is not years but days based on the day of the year.

var dFilterDate1 = DateTime.Now.AddYears(-2); 
var dFilterDate2 = dFilterDate1.AddDays(-4);
var dFilterDate3 = dFilterDate1.AddDays(-(dFilterDate1.DayOfYear - 1));
var dFilterDate4 = dFilterDate3.AddYears(-1);

Second, C# does not have a built-in equivalents for weeks so I had to copy some helper functions from SO.

I also had to breakdown the code into lots of temp variables and this is what I got:

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        var dFilterDate1 = DateTime.Now.AddHours(-4); // UTC to EST for apples to apples testing
        var dFilterDate2 = dFilterDate1.AddDays(-4);
        var dFilterDate3 = dFilterDate1.AddDays(-(dFilterDate1.DayOfYear - 1));
        var dFilterDate4 = dFilterDate3.AddYears(-1);
        
        
        var dFilterDate5temp1 = ((int)dFilterDate1.DayOfWeek + 1) - 1; // this is for readability.  -1 is needed for C# vs VB
        var dFilterDate5temp2 = (int)dFilterDate4.DayOfWeek + 1;
        var dFilterDate5temp3 = dFilterDate4.AddDays(-dFilterDate5temp2);
        var dFilterDate5temp4 = GetWeekOfYear(dFilterDate1);
        var dFilterDate5temp5 = dFilterDate5temp3.AddDays(7 * (dFilterDate5temp4 - 1));
        var dFilterDate5 = dFilterDate5temp5.AddDays(dFilterDate5temp1);
        
        
        Console.WriteLine(dFilterDate1);
        Console.WriteLine(dFilterDate2);
        Console.WriteLine(dFilterDate3);
        Console.WriteLine(dFilterDate4);
        Console.WriteLine(dFilterDate5);
    }

    public static int GetWeekOfYear(DateTime dt)
    {
        DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
        Calendar cal = dfi.Calendar;
        return cal.GetWeekOfYear(dt, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
    }
}

You can test it here https://dotnetfiddle.net/LFYfyG

I get this as output:

9/23/2021 11:58:07 AM
9/19/2021 11:58:07 AM
1/1/2021 11:58:07 AM
1/1/2020 11:58:07 AM
9/23/2020 11:58:07 AM

And this is from the VB6:

VB6 Results

This is my VB6 code that shows how I broke this down

Private Sub Command1_Click()

   Dim dFilterDate1 As String
   Dim dFilterDate5temp As Integer
   
   dFilterDate1 = DateAdd("yyyy", -1, Now) ' I tried many different values for this - this is one year from now so it is not exactly the same as the C# example
   dFilterDate2 = DateAdd("d", -4, dFilterDate1)
   dFilterDate3 = DateAdd("y", -(DatePart("y", dFilterDate1) - 1), dFilterDate1)
   dFilterDate4 = DateAdd("yyyy", -1, dFilterDate3)
   dFilterDate5 = DateAdd("d", Weekday(CDate(dFilterDate1)) - 1, CDate(DateAdd("ww", Format(CDate(dFilterDate1), "ww") - 1, CDate(CDate(dFilterDate4) - Weekday(CDate(dFilterDate4))))))
   
   dFilterDate5temp1 = Weekday(CDate(dFilterDate1), vbSunday) - 1
   
   dFilterDate5temp2 = Weekday(CDate(dFilterDate4), vbSunday)
   
   dFilterDate5temp3 = CDate(dFilterDate4) - dFilterDate5temp2
   
   dFilterDate5temp4 = Format(CDate(dFilterDate1), "ww")
   
   dFilterDate5temp5 = DateAdd("ww", dFilterDate5temp4 - 1, CDate(dFilterDate5temp3))
   
   dFilterDate5New = DateAdd("d", dFilterDate5temp1, CDate(dFilterDate5temp5))

   
   Text1.Text = dFilterDate1
   Text2.Text = dFilterDate2
   Text3.Text = dFilterDate3
   Text4.Text = dFilterDate4
   Text5.Text = dFilterDate5
   Text6.Text = dFilterDate5New

End Sub

The hardest parts were Weekday and WeekOfTheYear that do not exist directly in C#. Some of the solutions I found on SO were wrong so it lead me down the wrong path. Note that the answer will be different based on what is the first day of the week.

Update: I just noticed that I was testing the VB code with last year so the values are not the same. Here is the VB6 code with the values from Now. VB6 Results from Now

Also, VB6 is running on my laptop in the EST timezone while dotnetfiddle is always UTC. I just subtracted 4 hours so the times would be the same.

eglease
  • 2,445
  • 11
  • 18
  • 28
  • 1
    You can put all of C# code on one line but I do not recommend it as it make is very difficult to debug. – eglease Sep 23 '21 at 16:08
  • Your `+1) -1 ` is unnecessary, also it’s straight forward framework code that doesn’t differ between languages. – jmoreno Oct 01 '21 at 12:14
  • It is not necessary. I put it there to show that I am doing it on purpose. I belive the constants are different between vb6 and .net. – eglease Oct 01 '21 at 17:07