How to Convert Date Object with Format "MM/dd/yy hh:mm:ss tt" to DateObject with Format "dd/MM/yy
I have googled alot and tried lot of solutions but nothing is working for me.For Ex i Have tried below :
public static DateTime ParseDateToSystemFormat(DateTime date)
{
IFormatProvider culture = new CultureInfo("en-GB", true);
DateTime dt = DateTime.ParseExact(date.ToString("dd/MM/y开发者_Python百科yyy"),
"dd/MM/yyyy",
culture,DateTimeStyles.NoCurrentDateDefault);
return Convert.ToDateTime(dt,culture);
}
If anyone have solved this please let me know.
Date objects do not have formatting associated to them - you only use formatting for display.
When it is time to display the DateTime
object, use either custom or standard format strings to format the display to your liking.
What you are doing here:
DateTime dt = DateTime.ParseExact(date.ToString("dd/MM/yyyy"),
"dd/MM/yyyy",
culture,DateTimeStyles.NoCurrentDateDefault);
Is rather strange - you are getting a specific string representation of your DateTime
- date.ToString("dd/MM/yyyy")
, then parsing that string back to a DateTime
object. A bit of a long way to say DateTime dt = date;
, with clearing out the hours/minutes/seconds data.
If you simply want the date portion of a DateTime
, use the Date
property. It produces:
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
The internal representation of a DateTime
is always the same. There is no formatting attached to a DateTime
object.
If it is only a display problem, then convert the DateTime
to a string and display that string. You already know how to do it: Using ToString
and specifying the format you want to have.
精彩评论