7

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.

Deanna
  • 23,876
  • 7
  • 71
  • 156
eric_13
  • 383
  • 1
  • 8
  • 22
  • 6
    What are you trying to achieve? If you could tell us that, you would only need people who know .NET, not people who know .NET *and* VB6. – Jon Skeet Jun 26 '12 at 13:53

3 Answers3

12

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);
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • That works perfectly. Thank you. As a follow up I also have in a SQL string (still vb6) - Format(tAvailableDate, gDATEFORMAT) For C# i can simply make that - tAvailableDate . ? – eric_13 Jun 26 '12 at 14:14
  • To convert a `DateTime` object to a string, use the `ToString` method, such as: `tAvailableDate.ToString(gDATEFORMAT)` – Steven Doggart Jun 26 '12 at 14:21
11

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

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • 2
    +1. Yes, it makes no sense to convert to string with Format and then to try to add days. Days can be added to dates but not to strings. Probably it works with VB6, beacuse VB6 automatically converts the string back to `Date` and thus masks this error! But if it converts it back by using another culture than specified in `gDATEFORMAT` it could produce an error (by inverting days and months for instance). – Olivier Jacot-Descombes Jun 26 '12 at 13:59
  • Thanks for your help. That code worked perfectly. To do away with the Format in VB6 when writing my C# code I declared tAvailable date as a DateTime as opposed to a string. – eric_13 Jun 26 '12 at 14:16
  • As a follow up I also have in a SQL string (still vb6) - Format(tAvailableDate, gDATEFORMAT) For C# i can simply make that - tAvailableDate . ? – eric_13 Jun 26 '12 at 14:17
0

I have thought over your problem and there is an aspect that I missed yesterday. I thought that the Format function made no sense, but, even if it look strange, it can make sense. Let me explain.

In VB6 we have

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

Why does it look strange (or even wrong)? Now is a Date. Format converts this date to a String (well to a Variant containing a String to be precise), but DateAdd needs a Date parameter in order to be able to add days. DateAdd is declared like this:

Function DateAdd(Interval As String, Number As Double, Date)

Instead of giving a warning or a compiler error, VB6 silently converts this string back to a Date and passes it to DateAdd. So my first assumption was to just drop this Format.

BUT this Format can have a desired effect on the result, depending on how gDATEFORMAT is defined. If gDATEFORMAT contains only a date part, the format function will drop the time part! However this could simply be achieved by using the Date function instead of using the Now function in VB6

tAvailableDate = DateAdd("d", 21, Date) 

or DateTime.Today in .NET (C# or VB.NET).

But gDATEFORMAT could contain only month and year. VB6 (using my Swiss locale):

Date   ==>  27.06.2012
Format(Date,"MM.yyyy")   ==>  "06.2012"
CDate(Format(Date,"MM.yyyy"))   ==>  01.06.2012

As you can see, formatting the date would have the effect to return the first day of the current month in this case. By adding 21 days you would always get the 22nd of the current month. This is quite different than adding 21 days to the current date! In C# you could achieve the same with

DateTime today = DateTime.Today;
tAvailableDate = new DateTime(today.Year, today.Month, 22);

In order to decide which approach is correct, you must either know what gDATEFORMAT contains or, if this is variable, format the date and then parse the resulting string to get a date again.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188