Formatting dates?

E

Ed

I can't seem to figure out how to make a date come out in a specific
format. I parse thorugh a text file sent ot me by someone else. One
day the date will be 3/5/07, the next 5 Mar, the next 05 March 2007,
etc. I've tried capturing whatever I get into a variable Dim ThisDate
as Date, then using Format(ThisDate, "yyyymmdd") to get 20070305 - but
it didn't work like I hoped it would. Any suggestions?

Ed
 
J

Jean-Guy Marcil

Ed was telling us:
Ed nous racontait que :
I can't seem to figure out how to make a date come out in a specific
format. I parse thorugh a text file sent ot me by someone else. One
day the date will be 3/5/07, the next 5 Mar, the next 05 March 2007,
etc. I've tried capturing whatever I get into a variable Dim ThisDate
as Date, then using Format(ThisDate, "yyyymmdd") to get 20070305 - but
it didn't work like I hoped it would. Any suggestions?

How do you get the information in the ThisDate variable (Show us the whole
code).

Also, "it didn't work like I hoped it would" is not all that useful to those
wanting to help. What were the expectations? (I guess here it was a format
like "20070305") and what was the actual result, if any?


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Ed

Ed was telling us:
Ed nous racontait que :


How do you get the information in the ThisDate variable (Show us the whole
code).

Also, "it didn't work like I hoped it would" is not all that useful to those
wanting to help. What were the expectations? (I guess here it was a format
like "20070305") and what was the actual result, if any?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:http://www.word.mvps.org

Hi, Jean-Guy. To capture the information, I use a probably-very-
inefficient method, but it works for me! The whole thing is a daily
report, and I need to capture several data bits and set them into a
readable format. I set a range to all the report and put all the text
into a string. Then I use InStr to search for various bits and
stretch a range to cover the text I need.

With the date string, I was doing
ThisDate = strData
then various versions of the Format function (I can't find them - I
think I gave up and deleted my trials!). Something like this:

Sub TestDates()

Dim ThisDate As Date
Dim strData As String
Dim NewDate As Date

strData = "3/5/07"
ThisDate = strData
NewDate = Format(ThisDate, "yyyymmdd")
MsgBox NewDate

End Sub

With this code, on the "Format" line I get a Type Mismatch error.
With previous trials, I would either get the Type Mismatch, or it
would just return the date input in the incorrect format.

Ed
 
K

Karl E. Peterson

Ed said:
Sub TestDates()

Dim ThisDate As Date
Dim strData As String
Dim NewDate As Date

strData = "3/5/07"
ThisDate = strData
NewDate = Format(ThisDate, "yyyymmdd")
MsgBox NewDate

End Sub

With this code, on the "Format" line I get a Type Mismatch error.
With previous trials, I would either get the Type Mismatch, or it
would just return the date input in the incorrect format.

The "Type Mismatch" isn't occuring with the Format function, but with the assignment
of a String (that returned by Format) to a Date variable. VB doesn't know how to
coerce your custom format to a date. Which raises the obvious question, why are you
trying to do this, when it's *already* a date going into Format?
 
E

Ed

The "Type Mismatch" isn't occuring with the Format function, but with the assignment
of a String (that returned by Format) to a Date variable. VB doesn't know how to
coerce your custom format to a date. Which raises the obvious question, why are you
trying to do this, when it's *already* a date going into Format?
--
.NET: It's About Trust!
http://vfred.mvps.org- Hide quoted text -

- Show quoted text -

Hi, Karl.
The "Type Mismatch" isn't occuring with the Format function, but with the assignment
of a String (that returned by Format) to a Date variable. VB doesn't know how to
coerce your custom format to a date.

Ah - Format returns a _string_ variable. I *saw* "variable" and
*read* "variant" and didn't go further.

I took my test code and replaced the last line and it worked!

Sub TestDates()

Dim ThisDate As Date
Dim strData As String
Dim NewDate As Date

strData = "3/5/07"
ThisDate = strData
strData = Format(ThisDate, "yyyymmdd")
MsgBox strData

End Sub

Many thanks! (And I promise to read ALL the Help topic before posting
next time!)
Ed
 
K

Karl E. Peterson

Ed said:
Ah - Format returns a _string_ variable. I *saw* "variable" and
*read* "variant" and didn't go further.

To be wholly accurate, Format$() returns a String, Format() returns a Variant that
contains a String. The entire purpose of Format (used generically) is to produce
output. Nothing more. It's not designed for input to calculations or anything
else.
Many thanks! (And I promise to read ALL the Help topic before posting
next time!)

Actually, it was a bit of a brain twister, as Type Mismatch errors have become
almost something of the past ever since the introduction of ETC. Might actually
make a good question on one of those notorious "job interview" quizzes that get
posted from time to time, because identifying the problem wasn't entirely intuitive.
<g>
 

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