Date & DateTime datatypes format

G

Guest

Hi there,

My problem is the following: When I assign a custom formatted Date & Time to
a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46
[Format(Now(), "yyyy-MM-dd HH:mm")] now when I assign this to a variable of
type Date or DateTime this becomes 2005/06/07 03:46:00

The same code work flawlessly on various other machines, thus I presume this
is settings related. I know that the Time function in VB.NET reads from the
system time, which you set in Regional Settings under the Control Panel. I
did try to change the settings but it did not seem to work, unless I'm not
setting it up correctly.

Any ideas would be appreciated.

Thanx
 
C

Cor Ligthert

Ed,

Showing samples whith dates that can mean 6 july or june 7 is in my opinion
not the best way to show a problem. Can you show it with a date as december
31.

Cor
 
M

Mattias Sjögren

My problem is the following: When I assign a custom formatted Date & Time to
a Date variable it loses it’s formatting.

Why do you assign it to a Date _after_ you perform the formatting? If
you have Option Strict turned on you shouldn't even be allowed to
assign a String to a Date like that.

Ex. 2005-06-07 15:46
[Format(Now(), "yyyy-MM-dd HH:mm")] now when I assign this to a variable of
type Date or DateTime this becomes 2005/06/07 03:46:00

A Date (or DateTime) variable doesn't carry any formatting
information. You can display it in whatever format you want with the
Format function or DateTime.ToString().



Mattias
 
J

Jon Skeet [C# MVP]

Ed said:
My problem is the following: When I assign a custom formatted Date & Time to
a Date variable it loses it?s formatting. Ex. 2005-06-07 15:46
[Format(Now(), "yyyy-MM-dd HH:mm")] now when I assign this to a variable of
type Date or DateTime this becomes 2005/06/07 03:46:00

Yes, in the same way that if you parsed a string saying "10.00000" as a
double, you wouldn't keep the formatting from that either. The format
isn't any part of the DateTime logically - an instant in time doesn't
have a format associated with it any more than a number does.
The same code work flawlessly on various other machines, thus I presume this
is settings related.

Well, it would "work flawlessly" if the format you used happened to be
the system default one...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top