473,725 Members | 2,244 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 2477
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
7285
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
9761
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
10882
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
18720
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
24099
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
7964
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
35893
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
3789
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
16700
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
8874
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
8748
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
9248
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
9092
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...
1
6695
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
4506
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...
0
4775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3212
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
2151
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.