473,848 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Safe conversion from string to DateTime

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.ToDateT ime 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 it doesn't expose this functionality via API.
Nov 17 '05 #1
4 18174
use DateTime.Parse or DateTime.ParseE xact

Parse ignores leading and trailing white space, and unrecognized
characters if possible, and it fills in missing information with the
corresponding current date and time values. Parse will throw a
FormatException , though, if it's unable to decipher the string you send
to it.

DateTime.Parse will parse a valid date and time from a string. The
string must contain the representation of a date and time in one of the
formats described in the DateTimeFormatI nfo object.

For DateTime.ParseE xact, the string that you pass to it must exactly
match the format that you specify in the IFormatProvider parameter.

Example for DateTime.Parse

NewDate = DateTime.Parse( "10/27/61 08:47");
NewDate = DateTime.Parse( "10/1961");
NewDate = DateTime.Parse( "27 October 1961 8:47 pm");

IFormatProvider format = new
System.Globaliz ation.CultureIn fo("fr-FR", true);
string[] expectedFormats = {"g", "G", "f", "F"};

// This is DD/MM/YYYY format
NewDate = DateTime.ParseE xact("27/10/1961 08:47:00",
expectedFormats , format,
System.Globaliz ation.DateTimeS tyles.AllowWhit eSpaces);
Console.WriteLi ne("Parsed DD/MM/YY: {0}", NewDate.ToStrin g());
Example for DateTime.ParseE xact

try
{
NewDate = DateTime.ParseE xact(
"10/27/1961",
expectedFormats ,
format,
System.Globaliz ation.DateTimeS tyles.AllowWhit eSpaces);
Console.WriteLi ne(NewDate.ToSt ring());
}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
}

Hope this helps.

SCDeveloper
http://www.sharingcorner.com

Nov 17 '05 #2

Hi,

What you want cannot be done, how do you interprete 1/1/11 ?
You need to know the format the date is represented in, then you can use
the correct Format string in DateTime.ParseE xact()

Can you talk with the webform developer regarding the structure? you could
even provide him with the correct format string and then you can get as
input both the string to be converted as well as the srting representing the
date structure
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Hans Merkl" <ha********@yah oo.com> wrote in message
news:11******** *************** ******@40tude.n et...
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.ToDateT ime 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 it doesn't expose this functionality via
API.

Nov 17 '05 #3
I have tried DateTime.Parse but it threw an exception on "12 noon".

Hans
On 5 May 2005 07:15:22 -0700, SCDeveloper wrote:
use DateTime.Parse or DateTime.ParseE xact

Parse ignores leading and trailing white space, and unrecognized
characters if possible, and it fills in missing information with the
corresponding current date and time values. Parse will throw a
FormatException , though, if it's unable to decipher the string you send
to it.

DateTime.Parse will parse a valid date and time from a string. The
string must contain the representation of a date and time in one of the
formats described in the DateTimeFormatI nfo object.

For DateTime.ParseE xact, the string that you pass to it must exactly
match the format that you specify in the IFormatProvider parameter.

Example for DateTime.Parse

NewDate = DateTime.Parse( "10/27/61 08:47");
NewDate = DateTime.Parse( "10/1961");
NewDate = DateTime.Parse( "27 October 1961 8:47 pm");

IFormatProvider format = new
System.Globaliz ation.CultureIn fo("fr-FR", true);
string[] expectedFormats = {"g", "G", "f", "F"};

// This is DD/MM/YYYY format
NewDate = DateTime.ParseE xact("27/10/1961 08:47:00",
expectedFormats , format,
System.Globaliz ation.DateTimeS tyles.AllowWhit eSpaces);
Console.WriteLi ne("Parsed DD/MM/YY: {0}", NewDate.ToStrin g());
Example for DateTime.ParseE xact

try
{
NewDate = DateTime.ParseE xact(
"10/27/1961",
expectedFormats ,
format,
System.Globaliz ation.DateTimeS tyles.AllowWhit eSpaces);
Console.WriteLi ne(NewDate.ToSt ring());
}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
}

Hope this helps.

SCDeveloper
http://www.sharingcorner.com

Nov 17 '05 #4
1/1/11 is a tough one :-). I'd be happy to have something that matches
Outlook's functionality. It can handle "noon", "midnight" and I don't know
what else. I now can handle these with my code but people might come up
with other variations.

Unfortunately the mails are coming from a company that hasn't been
responsive to change requests in the past so I don't think I can change the
formats of the mails. It's one of my selling points that I can deal with
their mails as is...

On Thu, 5 May 2005 10:47:13 -0400, Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

What you want cannot be done, how do you interprete 1/1/11 ?
You need to know the format the date is represented in, then you can use
the correct Format string in DateTime.ParseE xact()

Can you talk with the webform developer regarding the structure? you could
even provide him with the correct format string and then you can get as
input both the string to be converted as well as the srting representing the
date structure
cheers,

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5170
by: Symon R | last post by:
This is a bit of a weird one that I haven't yet been able to solve - I'm hoping someone out there can disprove my findings and tell me where I've gone wrong! I have designed a web service that accepts messages from .NET clients. The web method call includes an object as one of it's parameters - this reflected object has been given a property called "FutureDelivery" and expects a DateTime value. The clients may be in different timezones...
5
1367
by: Jazper | last post by:
hi can anybody explain that to me: i did the following: - 1. input of char 244 ("ô") into string - 2. convert to getbytes - 3. convert back to string char 244 changed to char 63 ("?") why??? what can i do the get the same string...?
50
4978
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse method to accept formating that i need. if i do console.writeLine DateTime.Now.ToString it gives the format: YYYY-MM-DD HH:MM:SS TIA, Z.
18
2137
by: David Gacek | last post by:
I've tried several different ways all with the same reults. Invalid date Dim ddate As String ddate = "11/22/2004" Dim TryToConvert As Date = Date.Parse(ddate) Dim TryToConvert2 As Date = Convert.ToDateTime(ddate)
4
1506
by: apoc69 | last post by:
hi folks, is that possible in c#? : i have for example a simple field class, which contains a value, like this: public class Field { private object value;
4
3384
by: rz0 | last post by:
Hi all, This is a question about both C89 and C99 and is based on my partial reading of the standard drafts (one from before C89 but mainly N1124). If appropriate, please give a separate answer for each version of the language. Let's consider the conversion from a given floating real type to a specific integer type.
1
6283
by: amberz0389 | last post by:
I am trying to figure out the average time it takes to complete an event, so I need to convert my datetime to an integer. I am not sure how to do this. I was also wondering once this conversion is done, will I be able to do an average?
2
3057
by: Mike | last post by:
Hi, I have DateTime filed in db table. I'd like to search my db based on DateTime filed, actually to list all rows with particular date, for e.g. 16.11.2007... I send string to method for. eg. 16.11.2007 --short date and convert that to DateTime. After conversion it looks like 16.11.2007 0:00:00 How I can get short date like 16.11.2007 without 0:00:00 after
10
1502
by: Jeff | last post by:
Hey ..NET 2.0 I'm about to create 2 input parameters for a method. These are 2 DateTime parameters - one named "from" and another named "to"... - from and to DateTime values... Okay the trick is that the "from" DateTime should be like this 01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
0
9893
marktang
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...
0
9738
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,...
0
10664
jinu1996
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...
0
10348
tracyyun
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...
0
9500
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7894
isladogs
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...
0
5735
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4544
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
2
4137
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.