How to get date only if time is 00:00:00 from DateTime c#
Asked Answered
W

2

6

I want to convert DateTime object to string. What I want to achieve is following things:

  1. Get Date only out of it if Time is 00:00:00.
  2. Get Date and Time if both are present.
  3. I want to achieve this using CurrentCulture.DateTimeFormat and Convert.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.

Wigeon answered 19/8, 2015 at 8:19 Comment(3)
Why not if(datetime.time == TimeSpan.Parse("00:00:00")){//then}?Higa
Unfortunately you cannot create a branching format (do one thing if X, do another thing if not), you must implement this using normal C# code (if-statement).Campuzano
Simple, if(myTime != myTime.Date)Rishi
W
3

After a while a wrote method for myself.

public static string ConvertToMyDateTimeFormat(Nullable<DateTime> value, CultureInfo IFormateProvider)
        {
            if (value.HasValue)
            {
                if (value.Value.TimeOfDay.Ticks > 0)
                {
                    return value.Value.ToString(IFormateProvider);
                }
                else
                {
                    return value.Value.ToString(IFormateProvider.DateTimeFormat.ShortDatePattern);
                }
            }
            else
            {
                return string.Empty;
            }
        }
Wigeon answered 19/8, 2015 at 9:14 Comment(0)
H
9

Seems to me like this is pretty straight forward:

var dt = x.ExpectedJoiningDate;
string x = (dt.TimeOfDay == TimeSpan.Zero)?dt.ToShortDateString():dt.ToString();

PS: you can use a culture as parameter in ToString if you like. See https://msdn.microsoft.com/en-us/library/aa326720(v=vs.71).aspx for details on how to do this.


Tim made the remark that the OP wants to use Convert.ToString. It doesn't compute, so I refuse. Why doesn't it compute? Here's the code for Convert.ToString:

public static string ToString(DateTime value, IFormatProvider provider)
{
    return value.ToString(provider);
}

Yes people, that's basically the same.

That said, if you're stubborn, I guess you can implement IFormatProvider in your own little class, change the format provider based on the condition, then pass that instead of the default format provider. Then, congrats, you've created a lot of senseless code that gives the exact same results using Convert.ToString.

Handhold answered 19/8, 2015 at 8:23 Comment(3)
Although it's not really an answer since OP needs to use Convert.ToString for whatever reasons. But since there is no way to write a conditional format string which you could use for Convert.ToString this seems to be the only way.Godroon
I can see symbols I've never used before. :D. No, wait... I've used : for division.Higa
@TimSchmelter Good remark, so +1 for that. I've added an explanation.Handhold
W
3

After a while a wrote method for myself.

public static string ConvertToMyDateTimeFormat(Nullable<DateTime> value, CultureInfo IFormateProvider)
        {
            if (value.HasValue)
            {
                if (value.Value.TimeOfDay.Ticks > 0)
                {
                    return value.Value.ToString(IFormateProvider);
                }
                else
                {
                    return value.Value.ToString(IFormateProvider.DateTimeFormat.ShortDatePattern);
                }
            }
            else
            {
                return string.Empty;
            }
        }
Wigeon answered 19/8, 2015 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.