How to exclude seconds from DateTime.ToString()

前端 未结 7 509
逝去的感伤
逝去的感伤 2020-12-09 16:26

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 m

相关标签:
7条回答
  • 2020-12-09 16:46

    please test:

    DateTime.Now.ToString("MM/dd/yyyy hh:mm");
    
    0 讨论(0)
  • 2020-12-09 16:47

    You might want to do something like DateTime.Now.ToString("M/d/yyyy hh:mm");

    for more information look at Custom Date and Time Format Strings

    0 讨论(0)
  • 2020-12-09 16:50

    You need to pass in a format string to the ToString() function:

    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.

    0 讨论(0)
  • 2020-12-09 16:50

    You could use a format:

    DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
    
    0 讨论(0)
  • 2020-12-09 16:56

    Output it as short date pattern:

    DateTime.Now.ToString("g")
    

    See MSDN for full documentation.

    0 讨论(0)
  • 2020-12-09 17:06

    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:

    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:

    <TextBlock Text="{Binding CreationTimestamp, Converter={local:DateTimeToStringConverter DisplayStyle=DateAndTime, DisplaySeconds=False}}" />
    
    0 讨论(0)
提交回复
热议问题