Converting DateAdd and Format code from VB6 to C#

倾然丶 夕夏残阳落幕 提交于 2021-02-07 13:12:58

问题


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.


回答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);



回答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




回答3:


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.



来源:https://stackoverflow.com/questions/11208859/converting-dateadd-and-format-code-from-vb6-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!