How to format string and show only date when data type is not a true date/time using C#?

I have the following string sEndDate with a value of:   "01/06/2015 08:52:05"

How do I format the string to only show the date part of this even despite this being a string and not a true date/time?
LVL 1
stephenlecomptejrAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
You can either treat it as a string and do something like:
sEndDate = sEndDate.Substring(0, sEndDate.IndexOf(' '));

Open in new window

Or, parse it as a date and do:
sEndDate = DateTime.Parse(sEndDate).ToShortDateString();

Open in new window

That might need a little massaging depending on your regional settings.
0
 
stephenlecomptejrAuthor Commented:
Carl,

When I tried the code I get the following error:  Length cannot be less than zero.  (Please note attachment)

When I hover over sEndDate I get the value:  "01/06/2015 08:52:05"
Error.png
0
 
stephenlecomptejrAuthor Commented:
The second one - parse as a date worked for me.  Thank you for your reply
I will have another question regarding dates as well.  Please look out for in a few secs.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
stephenlecomptejrAuthor Commented:
Carl, sorry I was not specific enough but I also need this formatted to have 01/06/2015... and everything I try doesn't work.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Not sure what you mean. How do you want it formatting?
0
 
stephenlecomptejrAuthor Commented:
Never mind.  The following did this for me:

sEndDate = DateTime.Parse(sEndDate).ToString("MM/dd/yyyy");

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.