472,374 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

parse multiple date formats at once

Hi,

Is there a way to parse datetime in string with following possible formats
into a DateTime object?
- MM/DD/YYYY
- MM/DD/YY
- MM/YY
- MM/YYYY

This line of code would throw if I pass "08/03"
DateTime result = DateTime.Parse( s );

Thanks!
Nov 15 '05 #1
4 7218
REPLY :
instead of datatime use string to accomodate all your
formats then use the CONVERT function Convert.DateTime
(string);

-----Original Message-----
Hi,

Is there a way to parse datetime in string with following possible formatsinto a DateTime object?
- MM/DD/YYYY
- MM/DD/YY
- MM/YY
- MM/YYYY

This line of code would throw if I pass "08/03"
DateTime result = DateTime.Parse( s );

Thanks!
.

Nov 15 '05 #2
Zeng <zz*@nonospam.com> wrote:
Is there a way to parse datetime in string with following possible formats
into a DateTime object?
- MM/DD/YYYY
- MM/DD/YY
- MM/YY
- MM/YYYY

This line of code would throw if I pass "08/03"
DateTime result = DateTime.Parse( s );


Use DateTime.ParseExact, which takes an array of formats to try one by
one.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
It seems to work but the format list keep growing (see below), I really need
a simpler way to default the date to the first day of the month, and tell
the parser/convertor to aim for month and year if the input string has only
2 numbers. Is there a way to do that?

private static string[] s_Formats = new string[] {
@"MM/dd/yyyy", @"MM-dd-yyyy", @"MM.dd.yyyy", @"MM\dd\yyyy",
@"M/dd/yyyy", @"M-dd-yyyy", @"M.d.yyyy", @"M\d\yyyy",
@"MM/dd/yy", @"MM-dd-yy", @"MM.dd.yy", @"MM\dd\yy",
@"M/dd/yy", @"M-dd-yy", @"M.dd.yy", @"M\dd\yy",
@"MM/yyyy", @"MM-yyyy", @"MM.yyyy", @"MM\yyyy",
@"MM/yy", @"MM-yy", @"MM.yy", @"MM\yy",
@"M/yy", @"M-yy", @"M.YY", @"M\YY" };

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zeng <zz*@nonospam.com> wrote:
Is there a way to parse datetime in string with following possible formats into a DateTime object?
- MM/DD/YYYY
- MM/DD/YY
- MM/YY
- MM/YYYY

This line of code would throw if I pass "08/03"
DateTime result = DateTime.Parse( s );


Use DateTime.ParseExact, which takes an array of formats to try one by
one.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
Zeng <zz*@nonospam.com> wrote:
It seems to work but the format list keep growing (see below), I really need
a simpler way to default the date to the first day of the month, and tell
the parser/convertor to aim for month and year if the input string has only
2 numbers. Is there a way to do that?

private static string[] s_Formats = new string[] {
@"MM/dd/yyyy", @"MM-dd-yyyy", @"MM.dd.yyyy", @"MM\dd\yyyy",
@"M/dd/yyyy", @"M-dd-yyyy", @"M.d.yyyy", @"M\d\yyyy",
@"MM/dd/yy", @"MM-dd-yy", @"MM.dd.yy", @"MM\dd\yy",
@"M/dd/yy", @"M-dd-yy", @"M.dd.yy", @"M\dd\yy",
@"MM/yyyy", @"MM-yyyy", @"MM.yyyy", @"MM\yyyy",
@"MM/yy", @"MM-yy", @"MM.yy", @"MM\yy",
@"M/yy", @"M-yy", @"M.YY", @"M\YY" };


Well, you could start by replacing all slashes, backslahes and dots
with dashes, and then just use one of the "columns" in the above.
Alternatively, split on the various delimiters, see how many values
you've got, parse them with Int32.Parse and then call the DateTime
constructor that takes three int parameters.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
4
by: Matteo | last post by:
Hy everybody. I'm not a html writer, but a sysadmin who's trying to help a user able to compile an online form with IE but not with Mozilla (Moz1.6, Ns7.1, Firefox 0.8+) due to a javascript date...
7
by: memememe | last post by:
I need to be able to parse both of these dates with the same method, and return them as a DateTime object. Is there any methods that would do this blindly or do I need to provide the format? Feb...
4
by: Muscha | last post by:
Hello, I have a date string in the following format: "2003-10-29T17:44+00:00" When I put it to DateTime.Parse() method it throws an exception, how do I parse this date format? Thanks, /m
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
3
by: Leo Muller | last post by:
Hi, Very often I have a problem with date formats. There are cases I have to cast the date to text and from text to date on a different server. However, sometimes the date formats on the servers...
4
by: Harold Crump | last post by:
Greetings, This is not strictly a ASP.NET question, but I am hoping someone can help. I have an ASP.NET web app that used the DateTime.Parse and Compare methods a lot. As expected, the...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.