String to DateTime

N

nobody

I know that given a FormatString and a DateTime you can use
DateTime.ToString(...) to convert the DateTime to a String. My question is
how can you turn that around? Given a String and a FormatString, how can
you convert the String back to a DateTime? DateTime.Parse(...) doesn't use
the FormatString.

Now admitedly, if the format string is just "MM", it can't be done. But if
the format string is "yyyyMMdd", or "ddMMMyyyy hhmmsst", it should be able
to be done. DateTime.Parse(...) is pretty good at figuring out the format
of the String, but it chokes on dates in the format "yyyymmdd" which is a
pretty common storage format for dates. I can't just parse it myself (ok, I
could, but it would be more work than I'm willing to (aka "being paid to")
do.) The Format Strings are entered into the program by the users and could
be ANYTHING. I'd like to at least make a good attempt to read the DateTime
data using the user's format string (the data was written out using their
format string using DateTime.ToString(FormatString).

Currently I'm writing the data out using DateTime.ToString(...) and reading
it back in with DateTime.Parse(...). But if their format string is
"yyyyMMdd", then it writes it out fine, but chokes reading it back in.

Any help on this matter is greatly appreciated. Thanks!

-----
 
N

nobody

Doh! The next line down in the index in the help file. Think I need to
take a break, I'm getting too "tunnel" visioned. :) Thank you. That looks
like exactly what I was looking for. I wonder how "exact" it is... It
mentions that the DateString cannot contain leading, inner, or trailing
white space characters. If the user orginally used "MMM dd, yyyy" as their
format string, I wonder if DateTime.ParseExact(...) will be able to read it
back in using that format string. There's inner white spaces. I'll have to
play with it and see how well it works.

Now if only there were a Decimal.ParseExact... :) I think
MyDecimal.ToString("0.0%") works great, but Decimal.Parse("28.6%") chokes.
I'm just doing a DecimalString.Replace("%","") first.

-----
Ken Tucker said:
Hi,

Take a look at datetime.parseexact

Ken
---------------
nobody said:
I know that given a FormatString and a DateTime you can use
DateTime.ToString(...) to convert the DateTime to a String. My question is
how can you turn that around? Given a String and a FormatString, how can
you convert the String back to a DateTime? DateTime.Parse(...) doesn't use
the FormatString.

Now admitedly, if the format string is just "MM", it can't be done. But if
the format string is "yyyyMMdd", or "ddMMMyyyy hhmmsst", it should be able
to be done. DateTime.Parse(...) is pretty good at figuring out the format
of the String, but it chokes on dates in the format "yyyymmdd" which is a
pretty common storage format for dates. I can't just parse it myself
(ok,
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

Convert.ToDateTime(strDate)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************************************************
Think outside the box!
***************************************************************
 
N

nobody

I see where that takes the string to convert to a DateTime, but not where it
takes the Format String to use. How would it know the difference between
"20040315" and "15032004" without format strings of "yyyyMMdd" and
"ddMMyyyy" respectively?

The solution that Ken Tucker mentioned is working great for me.
DateTime.ParseExact takes the string to convert and a format string.

Cowboy (Gregory A. Beamer) said:
Convert.ToDateTime(strDate)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************************************************
Think outside the box!
***************************************************************
nobody said:
I know that given a FormatString and a DateTime you can use
DateTime.ToString(...) to convert the DateTime to a String. My question is
how can you turn that around? Given a String and a FormatString, how can
you convert the String back to a DateTime? DateTime.Parse(...) doesn't use
the FormatString.

Now admitedly, if the format string is just "MM", it can't be done. But if
the format string is "yyyyMMdd", or "ddMMMyyyy hhmmsst", it should be able
to be done. DateTime.Parse(...) is pretty good at figuring out the format
of the String, but it chokes on dates in the format "yyyymmdd" which is a
pretty common storage format for dates. I can't just parse it myself
(ok,
 
N

nobody

Is there an equivelent function for Decimals? Suppose (for some reason), a
Decimal is format like this:

Dim MyDecimal as Decimal = 53.95
Dim MyFormat as String = "$$0.00"
Dim MyString as String = MyDecimal.ToString(MyFormat)

Is there any way, given MyString ("$$53.95") and MyFormat ("$$0.00"), to
convert MyString back into a Decimal? There is no Decimal.ParseExact(...).

Ken Tucker said:
Hi,

Take a look at datetime.parseexact

Ken
---------------
nobody said:
I know that given a FormatString and a DateTime you can use
DateTime.ToString(...) to convert the DateTime to a String. My question is
how can you turn that around? Given a String and a FormatString, how can
you convert the String back to a DateTime? DateTime.Parse(...) doesn't use
the FormatString.

Now admitedly, if the format string is just "MM", it can't be done. But if
the format string is "yyyyMMdd", or "ddMMMyyyy hhmmsst", it should be able
to be done. DateTime.Parse(...) is pretty good at figuring out the format
of the String, but it chokes on dates in the format "yyyymmdd" which is a
pretty common storage format for dates. I can't just parse it myself
(ok,
 
A

Armin Zingler

nobody said:
Is there an equivelent function for Decimals? Suppose (for some
reason), a Decimal is format like this:

Dim MyDecimal as Decimal = 53.95
Dim MyFormat as String = "$$0.00"
Dim MyString as String = MyDecimal.ToString(MyFormat)

Is there any way, given MyString ("$$53.95") and MyFormat ("$$0.00"),
to convert MyString back into a Decimal? There is no
Decimal.ParseExact(...).


Why not manually remove the "$" from the string, then call decimal.parse?
afterwards

if mydecimal.tostring(<format>) = mystring then
'format is correct
end if


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
C

Cor Ligthert

Hi Armin,

I am almost sure there is an instruction for that, however it will not come
up in my mind.

Cor
 
N

nobody

That was just an example. In application, the format string might be
something supplied by the end user at runtime, or it may be a property on a
custom control I design that another developer is using. Either way, the
format string would be supplied by someone other than myself and could be
anything.
 
C

Cor Ligthert

Hi Odysee

I was hoping on Herfried, I put his name in the Subject probably he knows,
when not I will search again.

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
I was hoping on Herfried, I put his name in the Subject probably he knows,
when not I will search again.

I don't have a solution. There is a way to make 'Double.Parse' parsing
strings which include currentcy symbols by specifying
'System.Globalization.NumberStyles.AllowCurrencySymbol' in its 2nd
parameter 'style', but thiw won't work with "$$" prefixes, AFAIS.
 
A

Armin Zingler

nobody said:
That was just an example. In application, the format string might
be something supplied by the end user at runtime, or it may be a
property on a custom control I design that another developer is
using. Either way, the format string would be supplied by someone
other than myself and could be anything.

If it's not a valid format, recognized by the Parse function, you can not
use the Parse function.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
C

Cor Ligthert

Hi Odesee,

I had to think on using the VAL function and I knew there was a reletion to
Herfried.

Have a look if that is usable in this for you.

Cor
 
J

Jay B. Harlow [MVP - Outlook]

nobody,
Have you tried Decimal.Parse with the NumberStyles overload?

MyDecimal = Decimal.Parse(MyString, NumberStyles.Currency)

Not exactly ParseExact, however it should be what you need. (you may need to
use a different combination of NumberStyles).

NOTE: The double $$ causes problems you may need to clean the string first,
a single $ works as expected.

Hope this helps
Jay

nobody said:
Is there an equivelent function for Decimals? Suppose (for some reason), a
Decimal is format like this:

Dim MyDecimal as Decimal = 53.95
Dim MyFormat as String = "$$0.00"
Dim MyString as String = MyDecimal.ToString(MyFormat)

Is there any way, given MyString ("$$53.95") and MyFormat ("$$0.00"), to
convert MyString back into a Decimal? There is no Decimal.ParseExact(...).

Ken Tucker said:
Hi,

Take a look at datetime.parseexact

Ken
---------------
question
is doesn't
use But
if
is
 
N

nobody

MyDecimal..ToString("0.0%") is a valid format. It outputs .125 as 12.5%.
However Decimal.Parse("12.5%", Globalization.NumberStyles.Any) chokes and
says "input string was not in a correct format.".

Also, the user could have specified a format of "Total: $#,##0.00". The
resulting string should be able to be turned back into the original decimal
if you have the resulting string ("Total: $49.95") and the format string
("Total: $#,##0.00"). DateTime lets you do this with DateTime.ParseExact.
DateTime.ParseExact("Arrival: 27-Mar-2004", "Arrival: dd-MMM-yyyy", Nothing)
will return the DataTime #03/27/2004#.
 
A

Armin Zingler

nobody said:
MyDecimal..ToString("0.0%") is a valid format.

"0.0%" is a valid /format string/, but "12.5%" is not a valid, parsable
/decimal string/.
It outputs .125 as
12.5%. However Decimal.Parse("12.5%", Globalization.NumberStyles.Any)
chokes and says "input string was not in a correct format.".


Also, the user could have specified a format of "Total: $#,##0.00".
The resulting string should be able to be turned back into the
original decimal if you have the resulting string ("Total: $49.95")
and the format string ("Total: $#,##0.00"). DateTime lets you do
this with DateTime.ParseExact. DateTime.ParseExact("Arrival:
27-Mar-2004", "Arrival: dd-MMM-yyyy", Nothing) will return the
DataTime #03/27/2004#.

Right, because there's a DateTime.parseExact function. This is not available
for the Decimal data type, that's why it is not possible this way.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
C

Cor Ligthert

Hi Armin,

It is one of your statements, however I got no reaction on using the VAL for
this.
(I never used it however I saw it once)

Cor
 
N

nobody

You are restating what I've already said. In my previous post, I said:

Is there any way, given MyString ("$$53.95") and MyFormat ("$$0.00"), to
convert MyString back into a Decimal? There is no Decimal.ParseExact(...).

I already know there isn't a ParseExact(...) method for Decimal. My
question was: Is there any way, given MyString (such as "$$53.95") and
MyFormat (such as "$$0.00") to convert MyString back into Decimal?

It just seems that there should be a way. DateTime provides a means to do
this, it seems odd that Decimal wouldn't have provided a means.
 
N

nobody

I tried the VB command VAL(...) and it doesn't work. It chokes on both
"12.5%" and $49.95". I was really hoping for a method that took the
formatted string and the original Format String used to create that string,
and return the Decimal value originaly used to create the formatted string.
 

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

Similar Threads


Top