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
270 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wpf develop 5 32
Not needed 13 55
Path to Python 9 44
fomat Json objects 6 18
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now