简体   繁体   English

如何从DateTime c#时间00:00:00获取日期

[英]How to get date only if time is 00:00:00 from DateTime c#

I want to convert DateTime object to string. 我想将DateTime对象转换为字符串。 What I want to achieve is following things: 我想要实现的是:

  1. Get Date only out of it if Time is 00:00:00. 如果时间是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. 我想使用CurrentCulture.DateTimeFormatConvert.ToString(DateTime, IFormatProvider)实现这一点,否则我知道如何使用.ToString()扩展方法。

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 . 输出时间为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. 我想要09-Oct-2015 11:00 AM如果有时间和09-Oct-2015如果时间不存在的话。

But above line gives me only date even if time is present with date. 但即使时间与日期一致,上面的行也只给我日期。

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. PS:如果您愿意,可以在ToString中使用culture作为参数。 See https://msdn.microsoft.com/en-us/library/aa326720(v=vs.71).aspx for details on how to do this. 有关如何执行此操作的详细信息,请参阅https://msdn.microsoft.com/en-us/library/aa326720(v=vs.71).aspx


Tim made the remark that the OP wants to use Convert.ToString . 蒂姆说这个OP想要使用Convert.ToString It doesn't compute, so I refuse. 它没有计算,所以我拒绝。 Why doesn't it compute? 为什么不计算? Here's the code for Convert.ToString : 这是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. 也就是说,如果你很顽固,我想你可以在你自己的小类中实现IFormatProvider ,根据条件更改格式提供者,然后传递它而不是默认格式提供者。 Then, congrats, you've created a lot of senseless code that gives the exact same results using Convert.ToString . 然后,恭喜,您已经创建了许多无意义的代码,使用Convert.ToString提供完全相同的结果。

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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#实体datetime返回日期,但时间始终为00:00:00 - C# entity datetime returns date but time is always 00:00:00 C# 如何将本地日期转换为 UTC,时间为 00:00:00 和 23:59:59? - C# How to convert date local to UTC with time 00:00:00 and 23:59:59? 在C#中,从更少23:00:00到更大24:00:00之间获取最接近24:00:00的时间 - Get the closest time to 24:00:00 between less 23:00:00 to greater 24:00:00 in C# 从列表中将时间更接近00:00:00、06:00:00、12:00:00和18:00:00的最佳方法 - Best way to get closer time to 00:00:00, 06:00:00, 12:00:00 and 18:00:00 from list 如果时间为00:00,如何在Listview的Datetime中隐藏时间 - How to hide time from Datetime on Listview if time is 00:00 如何在C#中的DateTime中将输出转换为00:00格式 - How to Convert Output into 00:00 format in DateTime in c# c#字符串启动为“ 00-00-00-00-00-00-00-00-00-00-00-00” - c# string initiates to “00-00-00-00-00-00-00-00-00-00-00-00” 将C#日期时间转换为将MySQL 00:00:00转换为MySQL - Convert C# Datetime to 0000-00-00 00:00:00 to MySQL 有没有办法在SQLite中将只有日期存储为00:00:00 - Is there a way to store Only date in SQLite with time 00:00:00 四舍五入时间和日期C#-当时间= 00:00:00时,日期增加1的问题 - Rounding up time and dates C# - Issue with increasing date by 1 when time = 00:00:00
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM