473,503 Members | 4,692 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 'yyyymmddthhmmss.sssz' that I would like to convert to datetime. My previous attempts:

DateTime dt = DateTime.Parse(string);
DateTime dt = Convert.ToDateTime(string);
DateTime dt = XmlConvert.ToDateTime(string, format); where format = 'yyyymmddthhmmss.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 10827
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 'yyyymmddthhmmss.sssz' that I would like to convert to datetime. My previous attempts:

DateTime dt = DateTime.Parse(string);
DateTime dt = Convert.ToDateTime(string);
DateTime dt = XmlConvert.ToDateTime(string, format); where format = 'yyyymmddthhmmss.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.CreateNavigator 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.ParseExact() (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
// "20080129P015750.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 = "20071026T120000.000Z";
IFormatProvider US_Format = new System.Globalization.CultureInfo("en-US", true);
string format = "yyyyMMddTHHmmss".sssZ;
RefDateTimeStamp = DateTime.ParseExact(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.ToString("yyyymmddthhmmss.fff tt")

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

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

If nothing works try the below

Dim s As String = "20071026T120000.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.ToDateTime(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 = "20071026T120000.000Z";
IFormatProvider US_Format = new System.Globalization.CultureInfo("en-US", true);
string format = "yyyyMMddTHHmmss".sssZ;
RefDateTimeStamp = DateTime.ParseExact(string, format, US_Format);
Jan 30 '08 #10

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

Similar topics

2
2723
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...
3
51554
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
1627
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
3428
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...
5
24426
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
7968
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...
12
13507
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} ...
9
8225
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
2883
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...
0
7207
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,...
0
7291
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,...
1
7012
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
7468
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...
0
5598
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,...
0
4690
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...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
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...

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.