Home > Software design >  DateAdd and DatePart from VB6 to C#
DateAdd and DatePart from VB6 to C#

Time:09-26

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 = ?;

CodePudding user response:

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 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.

  • Related