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.