473,569 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert string to datetime

233 New Member
I have read through various posts on converting a string into the datetime format, but cannot resolve my situation. I have a string in the format of 'yyyymmddthhmms s.sssz' that I would like to convert to datetime. My previous attempts:

DateTime dt = DateTime.Parse( string);
DateTime dt = Convert.ToDateT ime(string);
DateTime dt = XmlConvert.ToDa teTime(string, format); where format = 'yyyymmddthhmms s.sssz'

I should note that the string value is being extracted from an Xml document. Any help would be appreciated.
Jan 29 '08 #1
9 10844
Lacutas
5 New Member
I have read through various posts on converting a string into the datetime format, but cannot resolve my situation. I have a string in the format of 'yyyymmddthhmms s.sssz' that I would like to convert to datetime. My previous attempts:

DateTime dt = DateTime.Parse( string);
DateTime dt = Convert.ToDateT ime(string);
DateTime dt = XmlConvert.ToDa teTime(string, format); where format = 'yyyymmddthhmms s.sssz'

I should note that the string value is being extracted from an Xml document. Any help would be appreciated.
Just a quick thought, are you removing the surround tag elements from your string before you try to parse it?
Jan 29 '08 #2
mcfly1204
233 New Member
Just a quick thought, are you removing the surround tag elements from your string before you try to parse it?
I am using XmlResolver.Cre ateNavigator to extract string values out via the xpath, so only the contents of the given tag are being extracted, not the tag in its entirety.
Jan 29 '08 #3
Plater
7,872 Recognized Expert Expert
I would recomend DateTime.ParseE xact() (Normally I would say .Parse() or .TryParse() but it didn't look like they supported custom formats like that)

Expand|Select|Wrap|Line Numbers
  1. // "20080129P015750.100-5" <-you cannot get better format then that?
  2. string format="yyyyMMddthhmmss.sssz";
  3. //NOTE: I had to fix your format, as you had "2 digit minutes" specified in there twice
  4. DateTime dt = DateTime.ParseExact(string, format, <some format provider>);
  5.  
Have to check in on what format provider you wanna use, msdn should help.
Jan 29 '08 #4
mcfly1204
233 New Member
// "20080129P01575 0.100-5" <-you cannot get better format then that?
Unfortunately this is someone's idea of a "standard".
Jan 29 '08 #5
Plater
7,872 Recognized Expert Expert
Did correcting the format string have any effect?
You could always write your own too if need be.
Jan 29 '08 #6
mcfly1204
233 New Member
Did correcting the format string have any effect?
You could always write your own too if need be.
It kept saying that the string was not a valid date format. I broke down and removed the extra characters so that it was simply 'yyyyMMddHHmmss '. Below did not work for me.

string = "20071026T12000 0.000Z";
IFormatProvider US_Format = new System.Globaliz ation.CultureIn fo("en-US", true);
string format = "yyyyMMddTHHmms s".sssZ;
RefDateTimeStam p = DateTime.ParseE xact(string, format, US_Format);
Jan 29 '08 #7
Plater
7,872 Recognized Expert Expert
Oh the Z was a literal and not the designation for gmt offset? The same with the T, it is not the AM/PM designator but ACTUALLY a T?
What is an example of the exact string you will be parsing?

EDIT: ok I found more problems with your date format string, and have corrected with the following example:
Expand|Select|Wrap|Line Numbers
  1. string s = "20071026A120000.000-5";//example string to parse
  2. IFormatProvider US_Format = new System.Globalization.CultureInfo("en-US", true);
  3. string format = "yyyyMMddtHHmmss.fffz";
  4. DateTime dt = DateTime.ParseExact(s, format, US_Format);
  5. MessageBox.Show(dt.ToString());
  6.  
Jan 29 '08 #8
leoiser
41 New Member
Try this

DateTime.Now.To String("yyyymmd dthhmmss.fff tt")

tt for AM/PM
Jan 30 '08 #9
leoiser
41 New Member
string = "20071026T12000 0.000Z";

In the above string what is 'T' stands for?

If nothing works try the below

Dim s As String = "20071026T12000 0.000Z" //Assuming T stands for time
s = s.Insert(4, "/")
s = s.Insert(7, "/")
s = s.Replace("T", " ")
s = s.Replace("Z", "")
s = s.Insert(13, ":")
s = s.Insert(16, ":")

MessageBox.Show (Convert.ToDate Time(s))

Is it helpful?

It kept saying that the string was not a valid date format. I broke down and removed the extra characters so that it was simply 'yyyyMMddHHmmss '. Below did not work for me.

string = "20071026T12000 0.000Z";
IFormatProvider US_Format = new System.Globaliz ation.CultureIn fo("en-US", true);
string format = "yyyyMMddTHHmms s".sssZ;
RefDateTimeStam p = DateTime.ParseE xact(string, format, US_Format);
Jan 30 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2736
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored in a querystring, I can't figure out how to convert it back to a CipherMessage.
3
51579
by: Vicki Carlsen | last post by:
Hi, What is the easiest way to convert a DateTime object to a long?? - And the other way back?? (For database use) Regards, Vicki
2
1628
by: ad | last post by:
I use a textbox for user to enter a date. How can I determine if the string can convert to datetime.
3
3438
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a little bit complicated in this beautiful language to me .....
5
24448
by: Fabio | last post by:
Hi all! A simple question: I need to convert a DateTime var to a byte (or to a Int64). How? Thanks! --
3
7973
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object of FileStream type and the mode access. There is a function Convert.ChangeType(object value, Type conversionType), but I cant write for example:...
12
13518
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
9
8233
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
3
2886
by: tanishka singh | last post by:
How do you convert string from database into date in asp.net? Dim output = (From tlb In obj.SelectRecordAll_SalesInquiryRegister _ Join tlb1 In LatestPOs On tlb.SIR_OfferNo Equals tlb1.OfferNo _ And tlb.SIR_AlterationNo Equals tlb1.PORevNo _ Order By tlb.SIR_Flag _ ...
0
7698
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...
0
7612
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...
0
7924
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. ...
0
8122
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...
1
7673
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...
0
6284
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...
1
5513
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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

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.