473,748 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting a date string to a DateTime. Example please

I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
convert to a DateTime value.
I know this is a standard problem, but please don't just link to all the
MSDN pages regarding Parse() and ParseExact().
I've been there and read all the information about using IFormatProvider s,
DateTimeStyles, CultureInfo.... ...

Bottom line is that I can't get it working, so could someone please just
post 5-10 lines of working code that is needed to perform this simple
conversion?

Thank You,
Kim
Nov 17 '05 #1
10 15548
"Kim Hellan" <so*****@nowher e.com> wrote in message
news:ec******** ******@TK2MSFTN GP12.phx.gbl...
I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
convert to a DateTime value.
No problem.
I know this is a standard problem, but please don't just link to all the
MSDN pages regarding Parse() and ParseExact().
Er, well that's where the answer lies...
I've been there and read all the information about using IFormatProvider s,
DateTimeStyles, CultureInfo.... ...
Really? Maybe you were having a bad day...
Bottom line is that I can't get it working, so could someone please just
post 5-10 lines of working code that is needed to perform this simple
conversion?


string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse( strDate);
Nov 17 '05 #2
>>I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
convert to a DateTime value.


No problem.
I know this is a standard problem, but please don't just link to all the
MSDN pages regarding Parse() and ParseExact().


Er, well that's where the answer lies...
I've been there and read all the information about using
IFormatProvider s, DateTimeStyles, CultureInfo.... ...


Really? Maybe you were having a bad day...
Bottom line is that I can't get it working, so could someone please just
post 5-10 lines of working code that is needed to perform this simple
conversion?


string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse( strDate);


Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month and
day?
Assuming that the application runs on computers with different Windows
settings for date/time format.
Nov 17 '05 #3
Kim,
string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse( strDate);


Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month and
day?


It does not, therefore you should only use the DateTime.Parse (or if you use
VBNet the than easier one for that CDate), if it comes from an input control
as the textbox (and than it should be the installed culture way and go
automaticly).

If you use it on internet, than you should be sure that you have showed the
datetime format mask beside the box.

You can on Internet try to get the location from which it comes (that is not
standard) and than assume the culture, however this is in my opinion real
dangerous, because in by instance in Canada are two complete different date
format paterns used.

I hope this helps,

Cor
Nov 17 '05 #4
OOPs

Forgot what I wrote about VBNet, this human hit his head again to the same
stone. (A dutch phrase for "once hitten twice shy"). A donkey does not do
that.

:-)

Cor
Nov 17 '05 #5
>>> string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse( strDate);


Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month
and day?


It does not, therefore you should only use the DateTime.Parse (or if you
use VBNet the than easier one for that CDate), if it comes from an input
control as the textbox (and than it should be the installed culture way
and go automaticly).

If you use it on internet, than you should be sure that you have showed
the datetime format mask beside the box.

You can on Internet try to get the location from which it comes (that is
not standard) and than assume the culture, however this is in my opinion
real dangerous, because in by instance in Canada are two complete
different date format paterns used.

I hope this helps,


Thank you, but my initial question still stands.
Can anybody give me a C# EXAMPLE of how to parse the "13-10-05 10:19:26"
string.
The string always have the format "DD-MM-YY hh:mm:ss" and the parsing should
work on ANY machine, ANYWHERE in the world, regardless of culture/date
settings on the computer used.

Thank You!
Kim
Nov 17 '05 #6
"Kim Hellan" <so*****@nowher e.com> wrote in message
news:uc******** *******@tk2msft ngp13.phx.gbl.. .
Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month and
day?
Assuming that the application runs on computers with different Windows
settings for date/time format.


Apologies - I didn't read your original post well enough.

You're right that the data format is ambiguous as it stands - certainly not
Y2k-compliant.

However, if you're confident that the incoming string will always be in the
format you specified in your OP, the following is guaranteed to work:

string strDate = "13-10-65 10:19:26";
int intCenturyThres hold = 55; // amend as required
string strCentury = Convert.ToInt32 (strDate.Substr ing(6, 2)) >
intCenturyThres hold ? "19" : "20";
int intYear = Convert.ToInt32 (strCentury + strDate.Substri ng(6, 2));
int intMonth = Convert.ToInt32 (strDate.Substr ing(3, 2));
int intDay = Convert.ToInt32 (strDate.Substr ing(0, 2));
int intHour = Convert.ToInt32 (strDate.Substr ing(9, 2));
int intMinute = Convert.ToInt32 (strDate.Substr ing(12, 2));
int intSecond = Convert.ToInt32 (strDate.Substr ing(15, 2));
DateTime dtmDate = new DateTime(intYea r, intMonth, intDay, intHour,
intMinute, intSecond);

Nov 17 '05 #7
I think your main problem is that your format string is wrong:

DateTime dt = DateTime.ParseE xact(
"13-10-05 10:19:26",
"dd-MM-yy hh:mm:ss", null);

Console.WriteLi ne(dt.ToString( "F"));

Prints: Thursday, October 13, 2005 10:19:26 AM
Kim Hellan wrote:
string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse( strDate);

Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year,
month and day?


It does not, therefore you should only use the DateTime.Parse (or if
you use VBNet the than easier one for that CDate), if it comes from
an input control as the textbox (and than it should be the installed
culture way and go automaticly).

If you use it on internet, than you should be sure that you have
showed the datetime format mask beside the box.

You can on Internet try to get the location from which it comes
(that is not standard) and than assume the culture, however this is
in my opinion real dangerous, because in by instance in Canada are
two complete different date format paterns used.

I hope this helps,


Thank you, but my initial question still stands.
Can anybody give me a C# EXAMPLE of how to parse the "13-10-05
10:19:26" string.
The string always have the format "DD-MM-YY hh:mm:ss" and the parsing
should work on ANY machine, ANYWHERE in the world, regardless of
culture/date settings on the computer used.

Thank You!
Kim


--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #8

"Kim Hellan" wrote...
Can anybody give me a C# EXAMPLE of how to parse the "13-10-05 10:19:26"
string.
The string always have the format "DD-MM-YY hh:mm:ss" and the parsing
should work on ANY machine, ANYWHERE in the world, regardless of
culture/date settings on the computer used.


I believe this should work:
string strDate = "13-10-05 10:19:26";

System.Globaliz ation.DateTimeF ormatInfo di =
new System.Globaliz ation.DateTimeF ormatInfo();

di.FullDateTime Pattern = "dd-MM-yy HH:mm:ss";

DateTime d = DateTime.ParseE xact(strDate, "F", di);
// Bjorn A
Nov 17 '05 #9
This is exactly what I have been looking for.
So simple, and without all that unnecessary IFormatProvider and CultureInfo
stuff.

THANK YOU!
"James Curran" <Ja*********@mv ps.org> skrev i en meddelelse
news:ua******** ******@TK2MSFTN GP10.phx.gbl...
I think your main problem is that your format string is wrong:

DateTime dt = DateTime.ParseE xact(
"13-10-05 10:19:26",
"dd-MM-yy hh:mm:ss", null);

Console.WriteLi ne(dt.ToString( "F"));

Prints: Thursday, October 13, 2005 10:19:26 AM
Kim Hellan wrote:
> string strDate = "13-10-05 10:19:26";
> DateTime dtmDate = DateTime.Parse( strDate);

Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year,
month and day?

It does not, therefore you should only use the DateTime.Parse (or if
you use VBNet the than easier one for that CDate), if it comes from
an input control as the textbox (and than it should be the installed
culture way and go automaticly).

If you use it on internet, than you should be sure that you have
showed the datetime format mask beside the box.

You can on Internet try to get the location from which it comes
(that is not standard) and than assume the culture, however this is
in my opinion real dangerous, because in by instance in Canada are
two complete different date format paterns used.

I hope this helps,


Thank you, but my initial question still stands.
Can anybody give me a C# EXAMPLE of how to parse the "13-10-05
10:19:26" string.
The string always have the format "DD-MM-YY hh:mm:ss" and the parsing
should work on ANY machine, ANYWHERE in the world, regardless of
culture/date settings on the computer used.

Thank You!
Kim


--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 17 '05 #10

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

Similar topics

3
2186
by: Ed | last post by:
Hi there, My problem is the following: When I assign a custom formatted Date & Time to a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46 now when I assign this to a variable of type Date or DateTime this becomes 2005/06/07 03:46:00 The same code work flawlessly on various other machines, thus I presume this is settings related. I know that the Time function in VB.NET reads from the system time, which you set in...
3
1396
by: Gary Smith | last post by:
I my SQL Datareader I am loading the date data into text box. Here is my code txtActOpenDate.Text = MyDataReader("ActOpenDt").ToString() txtActOpenDate.Text is displaying the date and time. How to display the date alone here. Thanks for your advice.
1
1358
by: Glenn M | last post by:
I have a date stored as a string in the format mm/dd/yyyy. What is the easiest way to get this date converted to a datetime object so i can include it in the datediff function. also what is the method i should use to return a date string back from the datatime object, possibly in a different format such as dd/mm/yyyy glenn
3
12426
by: NateM | last post by:
How do I convert any given date into a milliseconds value that represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT? Is there an easy way to do this like Date in java? Thanks, Nate
9
12945
by: Alok yadav | last post by:
i am using a webservice in which a method is serach. i use this method which accept a argument of date type in dd/MM/yyyy formate. i have a textbox which accept the date from the user, when i convert textbox data into Datatime formate it converted into MM/dd/yyyy formate, but i have a requirement in dd/MM/yyyy formate. please help me, i am using c#.
16
1839
by: Bishman | last post by:
This is driving me mad, should be simple, How can I format a date string "01122006" to be "01/12/2006" I have tried string teststr = String.Format("{d}","01122006"); AND string teststr = String.Format("{##/##/####","01122006");
2
11181
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") pArray is a variant array containing a date string at pArray(4, iRecord) in the format "yyyy/mm/dd"
3
7159
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying to autoformat the contents of text entries in a GUI. For numbers, I'm converting the text representation to the appropriate type (using atoi, atof, ...) and converting the result back to text with the correct format (using sprintf). But this does...
2
4733
by: stainless | last post by:
I know this is probably simple but I cannot find a method of converting a date string into a format that matches the DatePicker format in C# eg string "20080131" converted to "31 January 2008" I tried datetime.parseexact but could not find the definition for the appropriate format string. This would be a very useful tool fo me. Any ideas,please?
0
8983
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...
1
9310
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,...
0
9236
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
8235
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
6792
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
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
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
3298
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
3
2206
bsmnconsultancy
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...

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.