Converting DateAdd and Format code from VB6 to C#

12,343

Solution 1

DateAdd is an old VB6 method that was carried over into VB.NET for backwards compatibility. You could get it to work in C# as well if you included the Microsoft.VisualBasic namespace in your C# project, but I wouldn't recommend using the method in C# or VB.NET. Here's how you should be doing it (it's easier to read too):

tAvailableDate = DateTime.Now.AddDays(21);

Solution 2

My VB6 is a bit rusty, but if I recall, you're trying to add 21 days. So here's what you want to do:

tAvailableDate = DateTime.Now.AddDays(21);

UPDATE

You mentioned that you converted the variable to a DateTime from a string. If you need to get it back to a string (which it looks like you might from another comment), then you want to call:

tAvailableDate.ToString("[format string]");

For help on formatting your string the way you want, see: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

Share:
12,343
eric_13
Author by

eric_13

Updated on June 06, 2022

Comments

  • eric_13
    eric_13 almost 2 years

    I have the following code in vb -

    tAvailableDate = DateAdd("d", 21, Format(Now, gDATEFORMAT))
    

    I am attempting to convert this into C#.

    I have converted this so far -

    tAvailableDate = DateAdd("d", 21, Format (DateTime.Now, Global.gDATEFORMAT));
    

    But I cannot find a replacement for the DateAdd() or Format() feature.

    Any ideas? Thanks.