I want to convert DateTime object to string. What I want to achieve is following things:
- Get Date only out of it if Time is 00:00:00.
- Get Date and Time if both are present.
- I want to achieve this using
CurrentCulture.DateTimeFormat
andConvert.ToString(DateTime, IFormatProvider)
, otherwise I know how to do this using.ToString()
Extension method.
I have tried following things:
Thread.CurrentPrincipal = principal;
CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.DateTimeFormat.ShortDatePattern = MPAResource.DateFormat;
culture.DateTimeFormat.LongTimePattern = "hh:mm:ss tt";
culture.DateTimeFormat.ShortTimePattern = "hh:mm:ss tt";
culture.DateTimeFormat.FullDateTimePattern = MPAResource.DateTimeFormat;
Thread.CurrentThread.CurrentCulture = culture;
Then:
string x = Convert.ToString(x.ExpectedJoiningDate, CultureInfo.CurrentCulture);
Output is 09-Oct-2015 11:00 AM
. I want 09-Oct-2015 11:00 AM
if time is there and 09-Oct-2015
if time is not there.
But above line gives me only date even if time is present with date.
if(datetime.time == TimeSpan.Parse("00:00:00")){//then}
? – Higa