简体   繁体   English

如何从DateTime.ToString()中排除秒

[英]How to exclude seconds from DateTime.ToString()

I am using DateTime.Now.ToString() in a windows service and it is giving me output like "7/23/2010 12:35:07 PM " I want to exclude the second part, displaying only up to minute. 我在Windows服务中使用DateTime.Now.ToString(),它给我的输出像“7/23/2010 12:35:07 PM”我想排除第二部分,只显示最多一分钟。

So how to exclude seconds from that format...? 那么如何从那种格式中排除秒......?

Output it as short date pattern: 将其输出为短日期模式:

DateTime.Now.ToString("g")

See MSDN for full documentation. 有关完整文档,请参阅MSDN

You need to pass in a format string to the ToString() function: 您需要将格式字符串传递给ToString()函数:

DateTime.Now.ToString("g")

This option is culture aware. 此选项具有文化意识。

For this kind of output you could also use a custom format string , if you want full control: 对于这种输出,如果您想要完全控制,您还可以使用自定义格式字符串

DateTime.Now.ToString("MM/dd/yyyy hh:mm")

This will output exactly the same regardless of culture. 无论文化如何,这都将完全相同。

您可以使用以下格式

DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");

请测试:

DateTime.Now.ToString("MM/dd/yyyy hh:mm");

You might want to do something like DateTime.Now.ToString("M/d/yyyy hh:mm"); 你可能想做一些像DateTime.Now.ToString("M/d/yyyy hh:mm");

for more information look at Custom Date and Time Format Strings 有关更多信息,请查看自定义日期和时间格式字符串

If you want to stay language independent, you could use the following code (maybe in an IValueConverter (see second code snippet)) to remove only the seconds part from the string: 如果您希望保持语言独立,可以使用以下代码(可能在IValueConverter中 (请参阅第二个代码段))仅从字符串中删除秒部分:

int index = dateTimeString.LastIndexOf(':');
if (index > -1) {
    dateTimeString = dateTimeString.Remove(index, 3);
}

Here's an implementation of the converter. 这是转换器的实现。

[ValueConversion(typeof(DateTime), typeof(string))]
public class DateTimeToStringConverter : Markup.MarkupExtension, IValueConverter {
    public DateTimeToStringConverter() : base() {
        DisplayStyle = Kind.DateAndTime;
        DisplaySeconds = true;
    }

    #region IValueConverter
    public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture) {
        if (value == null) return string.Empty;
        if (!value is DateTime) throw new ArgumentException("The value's type has to be DateTime.", "value");

        DateTime dateTime = (DateTime)value;

        string returnValue = string.Empty;

        switch (DisplayStyle) {
            case Kind.Date:
                returnValue = dateTime.ToShortDateString();
                break;
            case Kind.Time:
                returnValue = dateTime.ToLongTimeString();
                break;
            case Kind.DateAndTime:
                returnValue = dateTime.ToString();
                break;
        }

        if (!DisplaySeconds) {
            int index = returnValue.LastIndexOf(':');

            if (index > -1) {
                returnValue = returnValue.Remove(index, 3);
            }
        }

        return returnValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture) {
        throw new NotSupportedException();
    }
    #endregion

    public override object ProvideValue(IServiceProvider serviceProvider) {
        return this;
    }

    #region Properties
    public Kind DisplayStyle { get; set; }

    public bool DisplaySeconds { get; set; }
    #endregion

    public enum Kind {
        Date,
        Time,
        DateAndTime
    }
}

You can also use it in XAML as a markup extension: 您还可以在XAML中将其用作标记扩展:

<TextBlock Text="{Binding CreationTimestamp, Converter={local:DateTimeToStringConverter DisplayStyle=DateAndTime, DisplaySeconds=False}}" />

尝试DateTime.Now.ToShortDateString() + " " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM