473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

DateTime.Parse with custom date format

How do I use DateTime.Parse or DateTime.ParseExact to create a
DateTime object from a string?

I tried this without luck:
string dateString = "27-02-2007";
// DateTime dt = DateTime.Parse(dateString);
// Throws a FormatException exception
IFormatProvider culture = new CultureInfo("fr-FR", true);
DateTime dt = DateTime.ParseExact(dateString, "dd-mm-yyyy", culture);
Console.WriteLine(dt);

output shows : 1/27/2007 0:02:00

Which is not what I expect..

Lars

Feb 28 '07 #1
1 51265
sc******@yahoo.com wrote:
How do I use DateTime.Parse or DateTime.ParseExact to create a
DateTime object from a string?

I tried this without luck:
string dateString = "27-02-2007";
// DateTime dt = DateTime.Parse(dateString);
// Throws a FormatException exception
IFormatProvider culture = new CultureInfo("fr-FR", true);
DateTime dt = DateTime.ParseExact(dateString, "dd-mm-yyyy", culture);
Console.WriteLine(dt);

output shows : 1/27/2007 0:02:00

Which is not what I expect..

Lars
No, but it's exactly what you have written. ;)

"mm" is used to specify minutes, not months.

Use "dd-MM-yyyy" instead of "dd-mm-yyyy".

--
Göran Andersson
_____
http://www.guffa.com
Feb 28 '07 #2

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

Similar topics

15
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...
16
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...
2
by: Sterling Ledet | last post by:
I am trying to create a web service that takes a string from my web server in the following format: Mon, 6 Oct 2003 18:39:47 UTC and put's in a datetime so it can then be reformatted in C# as...
4
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...
4
by: dwight | last post by:
Is there a way using DateTime.Parse to validate a date in the following format? mm/dd/yyyy Thanks
26
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'...
11
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...
5
by: js | last post by:
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss". I need to parse the text using System.DateTime.Parse() function with custom format. I got an error using the following code. ...
1
by: schouwla | last post by:
How do I use DateTime.Parse or DateTime.ParseExact to create a DateTime object from a string? I tried this without luck: string dateString = "27-02-2007"; // DateTime dt =...
3
by: Peter Duniho | last post by:
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 DateTimeFormatInfo instance, in which I'd replaced the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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...

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.