Formatting a DateTime object/String to only show yyyy/mm/dd [modified]
-
Hi, I have a DateTime object, and my code correctly sets the year/month and day from a DateTime picker controll on my form. When I return the object and use .ToString, I get the following output:
19/08/2011 00:00:00
Now, I am not using or setting the time, so it is always 00:00:00. My question is how do I remove this time part from the string? 1) Is there a method from the DateTime object to do this, or, 2) Do I need to do this once I convert to string, and if so how? Thank you in advance, Regards, Stephen **EDIT Found out. .ToString(format code) **
modified on Friday, August 19, 2011 8:00 AM
-
Hi, I have a DateTime object, and my code correctly sets the year/month and day from a DateTime picker controll on my form. When I return the object and use .ToString, I get the following output:
19/08/2011 00:00:00
Now, I am not using or setting the time, so it is always 00:00:00. My question is how do I remove this time part from the string? 1) Is there a method from the DateTime object to do this, or, 2) Do I need to do this once I convert to string, and if so how? Thank you in advance, Regards, Stephen **EDIT Found out. .ToString(format code) **
modified on Friday, August 19, 2011 8:00 AM
Call
ToString("MM/dd/yyyy")
-
Call
ToString("MM/dd/yyyy")
-
Call
ToString("MM/dd/yyyy")
You could also use the short date format specifier:
ToString("d")
-
Hi, I have a DateTime object, and my code correctly sets the year/month and day from a DateTime picker controll on my form. When I return the object and use .ToString, I get the following output:
19/08/2011 00:00:00
Now, I am not using or setting the time, so it is always 00:00:00. My question is how do I remove this time part from the string? 1) Is there a method from the DateTime object to do this, or, 2) Do I need to do this once I convert to string, and if so how? Thank you in advance, Regards, Stephen **EDIT Found out. .ToString(format code) **
modified on Friday, August 19, 2011 8:00 AM
Just FYI there is a list of the formatting codes here: Formatting a DateTime for display - format string description[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
Just FYI there is a list of the formatting codes here: Formatting a DateTime for display - format string description[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
OriginalGriff wrote:
Just FYI there is a list of the formatting codes here: Formatting a DateTime for display - format string description[^]
Yes; I found them just after posting. Thank you, Stephen
-
loyal ginger wrote:
Call
ToString("MM/dd/yyyy")
ToString("dd/MM/yyyy")
is better in his case I think. :)
V.
-
Hi, I have a DateTime object, and my code correctly sets the year/month and day from a DateTime picker controll on my form. When I return the object and use .ToString, I get the following output:
19/08/2011 00:00:00
Now, I am not using or setting the time, so it is always 00:00:00. My question is how do I remove this time part from the string? 1) Is there a method from the DateTime object to do this, or, 2) Do I need to do this once I convert to string, and if so how? Thank you in advance, Regards, Stephen **EDIT Found out. .ToString(format code) **
modified on Friday, August 19, 2011 8:00 AM
.ToShortDateString()
is also an options -
Just FYI there is a list of the formatting codes here: Formatting a DateTime for display - format string description[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
Very nice tip, bookmarked :thumbsup: