I'm sure there's a good explanation for this, but I can't figure it out.
I tried using DateTime.Parse( ) with a custom DateTimeFormatI nfo instance,
in which I'd replaced the DateTimeFormatI nfo.FullDateTim ePattern property
with my custom format string:
DateTimeFormatI nfo dtfi =
(DateTimeFormat Info)DateTimeFo rmatInfo.Invari antCulture.Clon e();
dtfi.FullDateTi mePattern = "dd/MMM/yyyy:HH:mm:ss zzz";
DateTime dt = DateTime.Parse( "23/Mar/2007:13:22:28 -0600", dtfi,
DateTimeStyles. AdjustToUnivers al);
For some reason, that doesn't work. If I try the exact same format string
with DateTime.ParseE xact(), it works fine.
My expectation was that the Parse() method would try all of the various
format strings it knows about, which would include the FullDateTimePat tern
string I set. But apparently it doesn't do that.
Can anyone tell me what it _does_ do, and why it doesn't at least include
all of the patterns set within the format pattern properties given to it?
In my case, using ParseExact() is a reasonable work-around, but I'm
wondering if there's a way to do this using the Parse() method. It sure
seems like it ought to work.
Thanks,
Pete 3 21257
On Thu, 14 Jun 2007 12:51:24 -0700, Peter Duniho
<Np*********@nn owslpianmk.comw rote:
[...]
My expectation was that the Parse() method would try all of the various
format strings it knows about, which would include the
FullDateTimePat tern string I set. But apparently it doesn't do that.
Can anyone tell me what it _does_ do, and why it doesn't at least
include all of the patterns set within the format pattern properties
given to it?
Really? No one here knows enough about how DateTime.Parse( ) works to
explain why it doesn't match my input string to the format I've provided?
Or did my post just go unnoticed somehow?
Pete
On Jun 19, 4:28 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
Can anyone tell me what it _does_ do, and why it doesn't at least
include all of the patterns set within the format pattern properties
given to it?
Really? No one here knows enough about how DateTime.Parse( ) works to
explain why it doesn't match my input string to the format I've provided?
Or did my post just go unnoticed somehow?
It was certainly unnoticed by me.
I can't say I can really explain it, although the docs have a *hint*
about it:
<quote>
If you parse a date and time string generated for a custom culture,
use the ParseExact method instead of the Parse method to improve the
probability that the parse operation will succeed. A custom culture
date and time string can be complicated, and therefore difficult to
parse. The Parse method attempts to parse a string with several
implicit parse patterns, all of which might fail.
</quote>
Now, it's not clear what those "several implicit parse patterns" are,
but it sounds like it's not looking at the *patterns* from the format
provider you specify, just other bits (like the month names etc).
Sorry it's not a more helpful answer...
Jon
On Tue, 19 Jun 2007 00:33:34 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
[...]
I can't say I can really explain it, although the docs have a *hint*
about it:
<quote>
If you parse a date and time string generated for a custom culture,
use the ParseExact method instead of the Parse method to improve the
probability that the parse operation will succeed. A custom culture
date and time string can be complicated, and therefore difficult to
parse. The Parse method attempts to parse a string with several
implicit parse patterns, all of which might fail.
</quote>
Yeah, I saw that. It's in fact why I wound up getting to the ParseExact()
method so quickly; otherwise, I might have fumbled around longer than I
did. :)
Now, it's not clear what those "several implicit parse patterns" are,
but it sounds like it's not looking at the *patterns* from the format
provider you specify, just other bits (like the month names etc).
I agree. :) But I was left wondering, if it's not looking at the
patterns from the format provider, what is it looking at, and why do I
bother providing a format provider if it's not going to look at the
patterns in the format provider? Or maybe the question should be directed
the other way: why doesn't the format provider use its own patterns in
providing parsing help to the Parse() method (since it don't really
understand how the format provider works, I don't know which way the
question should be worded).
Sorry it's not a more helpful answer...
Well, at least it helps me feel better about not understanding it myself.
:)
Thanks,
Pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dan S |
last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling DateTime.Parse() and catching the FormatException but it seems this is a bit inefficient - catching the exception that is. There is some pretty obvious delay while it traces back up the call stack. Is there a better way? Something that returns a bool...
|
by: PK9 |
last post by:
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.
I am using C#
eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.
|
by: Fred Strauss |
last post by:
I am having an odd problem using the DateTime.ParseExact() method that
I'm hoping someone can help me with.
I have XML files that use 2 different date formats internally. I am
trying to use the same call to ParseExact to parse either format. I am
using C# to call the following form of ParseExact:
public static DateTime ParseExact(string, string, IFormatProvider,
DateTimeStyles);
|
by: Dave P. |
last post by:
I'm trying to parse a string into a DateTime object. I
want to try using ParseExact to see if I can improve
performance of a frequently-called method (i.e., please
don't suggest using Parse()). My code looks like this:
IFormatProvider fp = new System.Globalization.CultureInfo
("en-US");
DateTime dt = DateTime.ParseExact("Mar 9, 2005", "MMM d,
yyyy", fp);
|
by: Hans Merkl |
last post by:
Does anybody know of a library that can handle strings pf various formats
and conver them to a DateTime value? The strings are coming from a webform
and I can't restrict the input (it's not my form).
I have been using Convert.ToDateTime but it choked on "12:00 noon".
I am looking for a function that can make sense out of anything that looks
like a date/time. Outlook is pretty good at it when you enter an
appointment with the GUI, but...
| |
by: Peter Kirk |
last post by:
Hi there
I would like some help with parsing date strings to DateTime structures.
I can see that DateTime has Parse and ParseExact methods - but I am not sure
what is best for me to use, and what parameters I need to supply.
I reveive a string from a web-form, which would look something like this:
"31-12-1999 14:56:22"; and I need to parse it to a DateTime
|
by: nobody |
last post by:
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...
|
by: Reny J Joseph Thuthikattu |
last post by:
Hi,
I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a
miniute to it.How can i do that?
by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004
12:00 AM'
How do i do that?
Reny
---
|
by: Cor Ligthert |
last post by:
Hello everybody,
Jay and Herfried are telling me every time when I use CDate that using the
datetime.parseexact is always the best way to do String to datetime
conversions. They don't tell why only that I have to listen to them because
they know it better.
They told also that in a business situation it is better to use
datetime.parseexact for changing cultures and not to use the globalization
setting. I did not give them this sample,...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |