473,791 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert a string to a datetime using a pattern

Hi,

I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm".
How could I convert this string to DateTime type (I prefer somehow to
set this as a pattern if it is possible because I have other
possibilities as "dd~MM~yyyy , hh:mm")?

Thanks.

Nov 17 '05 #1
9 2483
string myString = "1-1-2000";
DateTime myDateTime = DateTime.Parse( myString);

Hope this helps,
Arjen

"Iwan Petrow" <xx****@abv.b g> schreef in bericht
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi,

I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm".
How could I convert this string to DateTime type (I prefer somehow to
set this as a pattern if it is possible because I have other
possibilities as "dd~MM~yyyy , hh:mm")?

Thanks.

Nov 17 '05 #2
"Iwan Petrow" <xx****@abv.b g> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm".
How could I convert this string to DateTime type (I prefer somehow to
set this as a pattern if it is possible because I have other
possibilities as "dd~MM~yyyy , hh:mm")?


DateTime.Parse( ...)
Nov 17 '05 #3
Thanks.

But I want culture invariant.

I've found a class DateTimeFormatI nfo which is good enough but I want
to forbid AM and PM. I use HH but it doesn't work (it accepts them).

Any ideas for forbidding them (AM and PM)?

Nov 17 '05 #4
Hi,

One of the overloads of DateTime.ParseE xact does it:

DateTime.ParseE xact( "31-.12-.2005 23:51", "dd-.MM-.yyyy hh-.mm" ,
DateTimeFormatI nfo.InvariantIn fo );

Note:
I wrote the above line from memory, it may have some error in the format,
check it first, also note that the casing is important MM=month and
mm=minutes

Cheers,

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


"Iwan Petrow" <xx****@abv.b g> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi,

I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm".
How could I convert this string to DateTime type (I prefer somehow to
set this as a pattern if it is possible because I have other
possibilities as "dd~MM~yyyy , hh:mm")?

Thanks.

Nov 17 '05 #5
Iwan Petrow <xx****@abv.b g> wrote:
But I want culture invariant.
In that case, pass the invariant culture into DateTime.ParseE xact.
I've found a class DateTimeFormatI nfo which is good enough but I want
to forbid AM and PM. I use HH but it doesn't work (it accepts them).

Any ideas for forbidding them (AM and PM)?


Have you tried using ParseExact rather than just Parse?

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

It works in most cases. But for this there is a problem:
DateTime.ParseE xact( "31\\12\\20 05", "dd\\MM\\yy yy" ,
DateTimeFormatI nfo.InvariantIn *fo );

It throws exception with message "string is not a date" or something
similar.
Why? Any solutions?

Nov 17 '05 #7
Hi,

haa , tricky one :) , but easy to explain

if you look the help of DateTimeFormatI nfo you will see at the botton that
there is a \c variant :
\ c Where c is any character. Displays the character
literally. To display the backslash character, use "\\".

So, if you change the expression to:

DateTime d = DateTime.ParseE xact( @"31\12\2005 ", @"dd\\MM\\yy yy" ,
DateTimeFormatI nfo.InvariantIn fo );

you will be ok, see that I use the @ at the start, IMO it's clearer that
using two \
cheers,

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


"Iwan Petrow" <xx****@abv.b g> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Thanks

It works in most cases. But for this there is a problem:
DateTime.ParseE xact( "31\\12\\20 05", "dd\\MM\\yy yy" ,
DateTimeFormatI nfo.InvariantIn fo );

It throws exception with message "string is not a date" or something
similar.
Why? Any solutions?
Nov 17 '05 #8


Vishal Mohan

mo**********@gm ail.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #9


*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #10

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

Similar topics

19
7290
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + '/' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + '/'...
4
9772
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
2
10883
by: Abhishek Srivastava | last post by:
Hello All, I wrote the following program. using System; using System.Globalization; public class ShortDateTest { public static void Main(string args)
5
18725
by: simon | last post by:
I have datetime variable: Datetime tsEndTime; Should I use (DateTime): tsEndTime=(DateTime)rdr.GetValue(15) or is better to use: tsEndTime=Convert.ToDateTime(rdr.GetValue(15))
5
24108
by: rsanan | last post by:
How do I convert a datetime from en-GB to en-US format here is my code - (not working for the clients outside of US) /*******************CODE*****************/ System.Globalization.CultureInfo myCI = new System.Globalization.CultureInfo("en-US", true); xwriter.WriteElementString(""xxx", System.DateTime.Parse(Convert.ToDateTime("16/2/2006").ToShortDateString(), myCI).ToString()); /*******************************************/
14
7974
by: Me | last post by:
Hi all I am getting a really bizzare error on when I convert a string into a datetime: The code is : DateTime dt1 = Convert.ToDateTime("10 Sep 2005"); Console.WriteLine(dt1.Year);
4
35898
by: tshad | last post by:
Is there any difference between convert.ToDateTime and System.DateTime.Parse? I am using them both and they seem the same. Is one better to use than another? Thanks, Tom
5
3793
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
7
16719
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be returned from the source. My first attempt was as follows; (please ignore that error handling is not present in this example) public T GetValue<T(string objName) { T results;
0
9669
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
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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
9029
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
7537
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
6776
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
5431
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
4110
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
2916
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.