Date without Time

I'd like to remove the time portion from this code...
 lbllastseen.Text = dr["Lastseen"].ToString();

Open in new window


Currently it displays the date and time.
zachvaldezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
Try this code:
YourDateTime.ToString("dd/MM/yyyy")

Open in new window

0
Thomas UCommented:
if the format is always the same (unfortunately the label is TEXT instead of TIME format) you could use LEFT and the char count, to only get the date.

regards
Thomas
0
MishaProgrammerCommented:
If you get not DateTime variable, you can convert to DateTime and then display in this format:
Convert.ToDateTime(dr["Lastseen"]).ToString("dd/MM/yyyy");

Open in new window

0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Chinmay PatelChief Technical NinjaCommented:
Hi Zachvaldez,

I am not sure about the original format of your date hence, try this:

DateTime lastSeen = DateTime.MinValue;
            if(DateTime.TryParse( dr["Lastseen"].ToString(), out lastSeen))
            {
                Console.WriteLine(lastSeen.ToShortDateString());
            }
            else
            {
                Console.WriteLine("Error in Input Date");
            }

Open in new window


Regards,
Chinmay.
2
it_saigeDeveloperCommented:
Either solution by Misha or Chinmay would work.  That being said, you would want to include a null check or null-conditional check as the datarow value could potentially be null; e.g. -
var converted = default(DateTime);
if (DateTime.TryParse(dr["LastSeen"]?.ToString(), out converted))
{
	Console.WriteLine(converted.ToShortDateString());
}
else
{
	Console.WriteLine("Error in Input Date");
}

Open in new window

-saige-
1
zachvaldezAuthor Commented:
format should be MM/dd/yyyy.
0
Chinmay PatelChief Technical NinjaCommented:
Hi zachvaldez,

Just change to
Console.WriteLine(lastSeen.ToString("MM/dd/yyyy"););

Open in new window


@it_saige,
Wouldn't TryParse take care of null value? :)

Regards,
Chinmay.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
@Chinmay,

TryParse does indeed take care of a null value, however, a NullReferenceException would be thrown if you try to use ToString() on a null.

-saige-
0
Chinmay PatelChief Technical NinjaCommented:
@it_saige,
Yes. My bad. ToString indeed will throw an exception. Please use

@zachvaldez,
dr["LastSeen"]?.ToString()

Open in new window


Or

dr["LastSeen"] as string

Open in new window


to avoid it.

Regards,
Chinmay.
0
zachvaldezAuthor Commented:
I need to change the award points because other answers were equally correct. Quick click Bad fingers on my part. Sorry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.