Solved

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

Posted on 2015-02-03
6
312 Views
Last Modified: 2015-02-03
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
Comment
Question by:stephenlecomptejr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40587493
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
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40587507
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
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 40587510
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40587555
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40587574
Not sure what you mean. How do you want it formatting?
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40587650
Never mind.  The following did this for me:

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

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question