• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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?
0
stephenlecomptejr
Asked:
stephenlecomptejr
  • 4
  • 2
1 Solution
 
Carl TawnSystems 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now