[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Extracting a date from a string with the format provided

JensB

5/9/2012 6:08:00 AM

Lets say we have: -

s$ = Format(date, f$)

We will be provided with s$ and f$, and need to extract out the date.

We can assume that f$ will be a valid Short Date Format string as used
in the Windows Regional Settings Control Panel application. We have no
control over what the format will be - we will merely be told what it
is.

Anyone have any thoughts as to how we could easily reverse-out the date
from the string?

--
Michael Cole


14 Answers

ObiWan

5/9/2012 8:22:00 AM

0


> Anyone have any thoughts as to how we could easily reverse-out the
> date from the string?

using CDate() :) ?


Dee Earley

5/9/2012 8:27:00 AM

0

On 09/05/2012 07:08, Michael Cole wrote:
> Lets say we have: -
>
> s$ = Format(date, f$)
>
> We will be provided with s$ and f$, and need to extract out the date.
>
> We can assume that f$ will be a valid Short Date Format string as used
> in the Windows Regional Settings Control Panel application. We have no
> control over what the format will be - we will merely be told what it is.
>
> Anyone have any thoughts as to how we could easily reverse-out the date
> from the string?

There is no built in (in VB6) function to do this.

When I had to do this, I wrote a custom function to walk the format
string and extract the values from that point in the input string given
the format character(s).
It then called DateSerial to return a single date value at the end.

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

JensB

5/9/2012 8:28:00 AM

0

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...

--
Michael Cole


ObiWan

5/9/2012 10:03:00 AM

0


> > using CDate() :) ?

> Try doing a CDate("2/3/2012") and tell me what the month is?

I know, sorry, I was joking :)

Seriously, as Deanna pointed out, there's no native method to do what
you want, so you'll need to write some function which, given a
formatted date and a format string will return the date as a "Date"
type; notice that writing such a function won't be so trivial since you
may have something like

fmtdate = "2/3/2012"
fmtstr = "General Date"

in such a case you'll need to leverage some APIs to retrieve the system
formats and then use them to extract the bits and pieces from your
string

Also, and since we're at this; you wrote about different formats (e.g.
UK, US) are you dealing with times too ? If so and if the data you're
dealing with come from "all around the world" you'll have your share of
head-scratching when dealing with the different timezones (also since
the format string won't tell you the TZ); in my case, I had to deal
with similar issues in a past and I decided that all the datetime
values had to be passed around using ISO format (YYYY-MM-DD hh:nn:ss)
and using the GMT timezone; that way it was easy retrieving the actual
date/time value and then (if needed) converting it to local TZ

Schmidt

5/9/2012 10:20:00 AM

0

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

Larry Serflaten

5/9/2012 12:47:00 PM

0

Michael Cole wrote:

> We will be provided with s$ and f$, and need to extract out the date.

This is getting to sound like 'someone' is still tackling course material...

Is this a problem in some course or a real world situation???

LFS

DaveO

5/9/2012 1:57:00 PM

0


"Michael Cole" <invalid@microsoft.com> wrote in message
news:jod1mh$29f$1@dont-email.me...
> Lets say we have: -
>
> s$ = Format(date, f$)
>
> We will be provided with s$ and f$, and need to extract out the date.
>
> We can assume that f$ will be a valid Short Date Format string as used in
> the Windows Regional Settings Control Panel application. We have no
> control over what the format will be - we will merely be told what it is.
>
> Anyone have any thoughts as to how we could easily reverse-out the date
> from the string?
>
> --
> Michael Cole

This is nasty, if you can get the dates provided in a standard format do so,
it'll save a ton of potential problems.

The conversion is easy enough where the date is all expressed in numbers but
what about this: "DD MMMM YYYY"
In the UK or US it'll return "09 May 2012" but from Spanish, German, French
or Welsh speakers: "09 Mayo 2012", "09 Mai 2012", "09 Mai 2012", "09 Mai
2012"
Ok that wasn't too bad, "May" is the same in German, French & Welsh. However
July is "Gorffennaf" in Welsh, "Juli" in German and "Juillet" in French.

FWIW there are well over 6000 living languages in use on this planet.
Creating lookup tables for just the ones using variations of the Latin
alphabet would be very tricky, covering all the others would be a nightmare.



If you know the language it might be possible to change the locale of the
computer, use CDate, then restore the locale. Regrettably playing with
system settings like that is frowned upon (just a bit!)

DaveO.


unknown

5/9/2012 4:44:00 PM

0

Karl E. Peterson

5/9/2012 7:18:00 PM

0

Farnsworth presented the following explanation :
> See ConvertAnyDateFormat() function here:
>
> http://groups.google.com/group/microsoft.public.vb.general.discussion/msg/497380...

Anyone know what's up with the banner up-top on that page?

The old Google Groups will be going away soon.
Switch to the new Google Groups.

Doesn't sound like a Good Thing, does it? :-(

--
..NET: It's About Trust!
http://vfre...


unknown

5/9/2012 7:53:00 PM

0

"Karl E. Peterson" <karl@exmvps.org> wrote in message
news:joefu5$c6k$1@dont-email.me...
> Farnsworth presented the following explanation :
>> See ConvertAnyDateFormat() function here:
>>
>> http://groups.google.com/group/microsoft.public.vb.general.discussion/msg/497380...
>
> Anyone know what's up with the banner up-top on that page?
>
> The old Google Groups will be going away soon.
> Switch to the new Google Groups.
>
> Doesn't sound like a Good Thing, does it? :-(

It's just a new user interface, more like the old interface with tree view
on the left. Click on "Switch to the new Google Groups" to see it, but it
seems that it's not working now. It was working few weeks ago when I last
checked it.