Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

    i am android and Ios developer, i would like to convert string to DateTime

    `string nodeValue = "2013-02-11T14:29:01-05:00";`

    The nodeValue comes from after converting DataSet to XMLDocument

    how can i get the DateTime value as 2/11/2013 2:29:01 PM?

    Help me please...


What I have tried:

i tried by using Convert.ToDateTime(nodeValue) but it is not returning expected result.
Posted
Updated 21-Feb-19 3:00am
Comments
W Balboos, GHB 21-Feb-19 8:03am    
Google it - not much harder than posting here.
And bookmark the anyplace that's a good reference.
Korlakunta 21-Feb-19 8:06am    
i tried to google it but i did not find expected solution
W Balboos, GHB 21-Feb-19 8:09am    
Expected Solution?
If you know what to expect then do it.
If you don't know how to do it how can you expect anything?

In either case - you call yourself a 'developer'. No - you want to be a developer. You've a ways to go.
Korlakunta 21-Feb-19 8:13am    
Expected Solution means after conversion string to datetime 2/11/2013 2:29:01 PM

Thank You
W Balboos, GHB 21-Feb-19 8:15am    
A developer needs to use their brains and figure things out. If you think you'll find everything exactly as you want it you are in the wrong business.

 
Share this answer
 
C#
DateTime myDate = DateTime.ParseExact("2013-02-11T14:29:01", "yyyy'-'MM'-'dd'T'HH':'mm':'ss", System.Globalization.CultureInfo.InvariantCulture);


I have used 2013-02-11T14:29:01 instead of 2013-02-11T14:29:01-05:00 and its showing date time as per the requirement.

Thank You
 
Share this answer
 
string nodeValue = "2013-02-11T14:29:01-05:00";
	DateTime result = DateTime.Parse(nodeValue);
	Console.WriteLine(result);


2/11/2013 1:29:01 PM

Be aware that if the string is not a valid date an exception will be thrown
in that case use TryParse
ring nodeValue = "2013-13-11T14:29:01-05:00";
	DateTime result;
	 DateTime.TryParse(nodeValue, out result);
	Console.WriteLine(result);


1/0001 12:00:00 AM
 
Share this answer
 
v3
Comments
Korlakunta 21-Feb-19 8:19am    
i am able to see result value as 2/12/2013 12:59:01 AM
Finally got solution

             string nodeValue = "2013-02-11T14:29:01-05:00";
             DateTimeOffset dtOffset;

             if(DateTimeOffset.TryParse(nodeValue,null,DateTimeStyles.None,out dtOffset))
             {
                 DateTime myDate = dtOffset.DateTime;

                    //output myDate
                   //2/11/2013 2:29:01 PM
             }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900