How to display short date from DateTime field in label?

后端 未结 4 1756
走了就别回头了
走了就别回头了 2021-01-14 09:13

I have a DateTime field in my SQL Server DB, and I would like to display just the date in short date form on a label in a WinForm (C#). I still get 06/01/2013 12:00:00, thou

相关标签:
4条回答
  • 2021-01-14 09:53

    You can also use:

    string shortDate = DateTime.Now.ToString("yyyy-MM-dd");

    Change the yyyy-MM-dd format for the one you're looking for.

    0 讨论(0)
  • 2021-01-14 09:57

    You can use DateTime.ToShortDateString:

    label_date.Text = myDateTimeField.ToShortDateString();
    

    Remarks:

    The value of the current DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread culture. The return value is identical to the value returned by specifying the "d" standard DateTime format string with the ToString(String) method.

    The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; for the de-DE culture, it is "dd.MM.yyyy"; for the ja-JP culture, it is "yyyy/M/d". The specific format string on a particular computer can also be customized so that it differs from the standard short date format string.

    0 讨论(0)
  • 2021-01-14 10:00

    You can use Data annotation in model (or in ViewModel) class like follows:

    [DataType(DataType.Date)]
    public DateTime DateOfExpiration { get; set; }
    
    0 讨论(0)
  • 2021-01-14 10:04

    check the sequence of the date - month - year the input in sql is diffrent

    0 讨论(0)
提交回复
热议问题