Converting DateAdd and formatting code from VB6 to C #

I have the following code in vb -

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

I am trying to convert this to C #.

I have converted it so far -

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

But I can not find a replacement for the DateAdd() or Format() function.

Any ideas? Thanks.

+6
source share
3 answers

DateAdd is an old VB6 method ported to VB.NET for backward compatibility. You can get it working in C # if you included the Microsoft.VisualBasic namespace in your C # project, but I would not recommend using this method in C # or VB.NET. Here's how you should do it (this is also easier to read):

 tAvailableDate = DateTime.Now.AddDays(21); 
+11
source

My VB6 is a little rusty, but if I remember, you are trying to add 21 days. So here is what you want to do:

 tAvailableDate = DateTime.Now.AddDays(21); 

UPDATE

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

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

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

+10
source

I thought about your problem, and there is an aspect that I missed yesterday. I thought the Format function didn't make sense, but even if it looks weird, it might make sense. Let me explain.

In VB6 we have

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

Why does this look weird (or even wrong)? Now is a Date . Format converts this date to String (well, a Variant containing a String , to be precise), but DateAdd needs the Date parameter to be able to add days. DateAdd declared as follows:

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

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

BUT this Format may have the desired effect on the result, depending on how gDATEFORMAT determined. If gDATEFORMAT contains only a part of the date, the format function will lose some of the time! However, this can simply be achieved 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 can only contain a month and a year. VB6 (using my Swiss language):

 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 will return the first day of the current month in this case. By adding 21 days, you will always receive the 22nd day of the current month. This is completely different than adding 21 days to the current date! In C #, you can achieve the same result with

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

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

0
source

Source: https://habr.com/ru/post/919035/


All Articles