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
306 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#: How do I test for null objects and a dub a value? 2 37
Need a quick overview of Selenium testing 2 35
MCAT Verbal Reasoning 2 20
Convert a string to date 4 40
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

732 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