|
-
Sep 11th, 2009, 01:05 AM
#1
Date formatting
A strange problem, this.
If I run the following:
Code:
Msgbox cdate("15 Aug 2009")
in an application on my dev PC, I return a messagebox showing the correct date, 15/08/09.
On a particular different PC, however, this returns a .Net error "Could not cast string to type..." etc.
On the other hand, if I run this:
Code:
Dim mystr As String
Dim mystrarr() As String
mystr = "15/08/2009"
mystrarr = Split(mystr, "/")
Dim mydate As New Date(mystrarr(2), mystrarr(1), mystrarr(0))
MsgBox(Format(mydate, "dd/MM/yy"))
then I return the correct result 15/08/09 on my PC, but 0/15/09 on the other.
If I change the last Format line, however, to read "dd-MMM-yy", then it returns the correct 15-Aug-09 on BOTH PCs.
It looks like it might be something related to the international setttings, American vs UK format for example, which is getting it confused, but these are both exactly the same. They have the same versions of the framework installed.
Is there any different date/time formatting within the .Net framework? I would have expected it to use the local Windows settings. Any other theories as to why the date cast would crash and the formatting doesn't necessarily work properly with the month?
-
Sep 11th, 2009, 01:16 AM
#2
Re: Date formatting
Making assumptions about formatting is always risky. CDate can only convert Strings to Dates using formats that the system understands, which may vary from system to system. You need to make a decision whether you want to accept the system defaults, in which case it's up to the user to ensure that they enter data in an acceptable format for their system, or else you need to decide on a specific format. If you choose the latter then you should use Date.ParseExact to specify the exact format you expect. As always, you should write your code defensively and handle any invalid data gracefully.
-
Sep 11th, 2009, 06:02 AM
#3
Re: Date formatting
Agreed. However, seems very unlikely to me that any PC would fail to understand "dd MMM yy", especially a standard-build company PC, and especially when it works on one but not another.
in this case, it was actually being pulled in as part of a string from another system in a specified format, and I was simply turning it into a date to store in a date variable.
Woe betide those who think they can get away without needing exception-catching in a simple 3-line date-conversion proc which always has a standard-format input.
-
Sep 11th, 2009, 08:01 AM
#4
Re: Date formatting
 Originally Posted by zaza
However, seems very unlikely to me that any PC would fail to understand "dd MMM yy"
Obviously it's more likely than you thought. There must be something different about those systems, otherwise they wouldn't behave differently. You simply can't afford to assume when it comes to date formats.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|