Schmidt
5/9/2012 10:20:00 AM
Am 09.05.2012 10:27, schrieb Michael Cole:
> ObiWan explained :
>>> Anyone have any thoughts as to how we could easily reverse-out the
>>> date from the string?
>>
>> using CDate() :) ?
>
> And if the format provided is not the same as the local machine - i.e.,
> the local machine is using UK Date fomats and the format passed is the
> US Date format?
>
> Try doing a CDate("2/3/2012") and tell me what the month is?
>
> There was a reason why I said the format was being passed...
In case you rely on the system-setting-formatstring,
you would have to determine the "Month/Day/Year"
(or Day/Month/Year) Order at the time you do your
Date-Serializing into a String (meaning you will
need to save Extra-Data with regards to the
"Order of the Date-Format-Parts" along with your
Dates in this Client-Session, to be able to convert
your DateString back correctly into a VB-Date later on) ...
this is the Custom-Approach Deanna was suggesting.
However, if the background of your question
is, how to serialize a VB-(Dbl-based)Date into
a reliable (internationally robust) Date-String,
you can choose the IS0-8601 Date-Format, which
VBs CDate-Function is able to convert back without
any glitches, independent of the current User- or
System- DateFormat-Settings.
So if your question is basically along the lines of
"how to reliably serialize/deserialize Double-Values"
(where one knows, that Str(...) and Val(...) are
the Functions of choice in an internationalized
Application) - the combination of Format$ and
CDate can ensure a similar behaviour, when one
is using the *right* Format-Description-Param:
Another advantage of the ISO-Format is, that its
String-Representation behaves nicely also in Sort-
Algorithms.
Private Sub Form_Load()
'IS0-8601 Date-Format-Constants
Const Date8601$ = "YYYY\-MM\-DD hh\:mm\:ss"
Const ShortDate8601$ = "YYYY\-MM\-DD"
Dim D1 As Date, D2 As Date, S As String
D1 = Now
S = Format$(D1, Date8601)
Debug.Print D1, S
D2 = CDate(S)
Debug.Print D1 = D2
End Sub
Olaf