-2

I came from me last post

c# change date type format to ddmmyyyy

I have to work in a VB.Net WinForm Application I have a problem with Date or DateTime type.

I usually work with C#, but now I have this problem, I am sure is a common problem that others programmers came acrross.

I have defined in control panel my datetime format like "dd/MM/yyyy". Also y check in Regedit as it says in here

If, In my WinForm, I define a variable like this i C#

DateTime aa = DateTime.Now

DateTime.Now retrieves current date in format "dd/MM/YYYY" as spected.

But if I ask the same to VB.Net like this

Dim val As New Date
 val = Date.Now

val has the current date in format "MM/dd/yyyy"

Even if i set

val = Date.Now.ToString("dd/MM/yyyy")

val keeps format as "MM/dd/yyyy"

Every date I assign to a variable Datetime, it automatically convert format "dd/MM/yyyy" to "MM/dd/yyyy".

How can i configurate VB.Net to accept Datetime type as C# Does? I need it to take it from Control Panel.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Diego
  • 2,238
  • 4
  • 31
  • 68
  • 3
    `DateTime.Now` does not return the date/time in _any_ format - it's a raw date/time value. Formatting only makes sense when you turn the raw value into a string representation (which is what printing it out does). – xxbbcc Nov 15 '17 at 00:19
  • `Date` in VB is just an alias to `DateTime`, thus `Date.Now` and `DateTime.Now` are identical, and your observations must be influenced by something you aren't showing here. Neither has a format of its own. – Matt Johnson-Pint Nov 15 '17 at 00:31
  • You are chasing a problem that does not exist. VS use the invariant culture in the VB IDE (which may have changed in the very recent versions). Dates do not have a format - a format is just how a date value is ***displayed*** to humans – Ňɏssa Pøngjǣrdenlarp Nov 15 '17 at 00:33
  • 1
    When you do `Date.Now.ToString("dd/MM/yyyy")` you claim *val keeps format as "MM/dd/yyyy"* Are you sure? `ToString()` returns a string, not a date, so it doesn't make much sense. – derloopkat Nov 15 '17 at 00:34
  • 3
    The difference is caused by the fact that VB supports literals for the `DateTime` type while C# does not. When you view the value of a `DateTime` in the Watch window or the like in a C# project, you see the result of calling `ToString` on it. That will honour the system settings. In VB, you will actually see the value as a literal, which you can tell by the fact that it is wrapped in '#' delimiters. A `Date` literal is ALWAYS displayed as M/dd/yyyy. It doesn't honour system settings because, if it did, the same code would work differently on different machines. – jmcilhinney Nov 15 '17 at 00:38
  • 1
    This is not an issue though. The value of the `DateTime` is still exactly the same regardless. You're trying to solve a problem that doesn't exist. .NET `DateTime` values are stored as numbers so system settings has no impact. It's only when you need to display a date as text that format matters and then you'll call `ToString` and that will produce exactly the same result in both languages. – jmcilhinney Nov 15 '17 at 00:40
  • By the way, this question is definitely not a duplicate. It has nothing to do with `Strings` and relates specifically to how the debugger represents `DateTime` values. – jmcilhinney Nov 15 '17 at 00:41
  • @jmcilhinney - I have reopened the question. Please give an answer WRT the debugger and VB literals. That is good insight. Thanks. – Matt Johnson-Pint Nov 15 '17 at 01:54

1 Answers1

3

There is no actual issue here but the perceived difference is due to the fact that VB supports Date literals where C# does not. Both C# and VB support literals for numeric types, strings, Booleans, etc.

var str = "Hello World";
var num = 100;
var flag = true;
Dim str = "Hello World"
Dim num = 100
Dim flag = true

Only VB supports literals for dates though. For instance, in C#, you would have to do something like this for a hard-coded date:

var dt = new DateTime(1969, 6, 19);

You can do basically the same thing in VB:

Dim dt As New DateTime(1969, 6, 19)

but you can also do this:

Dim dt = #6/19/1969#

When you use a date literal in VB, you ALWAYS use M/dd/yyyy format. It simply couldn't make use of system settings because that would mean that the same code would behave differently on different machines.

Now, when you run a project in the debugger and use the Watch window or the like to view the value of a variable or other expression, the debugger will display a literal value if the type supports it. If the type doesn't support literals, you will see the result of calling ToString on the object. That means that the debugger will always show you something in M/dd/yyy format for a DateTime value in a VB project because it's showing you a literal Date, while what it shows you in a C# project depends on your system settings because it's the result of calling ToString on a DateTime.

This is not an issue at all though. The actual value of the DateTime variables doesn't change. They don't actually contain a format at all. They're just numbers in the system. Format is only an issue when you want to represent the value as text. When you need to do that, you'll call ToString on your value and that will honour the system settings, whether in VB or C#.

Note that I have used the terms Date and DateTime in this answer. DateTime is a .NET type while Date is an intrinsic VB data type. Both languages support literals for their intrinsic data types but only VB has an intrinsic data type for dates, which is why only VB supports literal dates. Just as int in C# and Integer in VB are aliases for the Int32 type, so Date in VB is an alias for the DateTime type, but there is no equivalent in C#.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46