【问题标题】:Changing the type of Datetime in database更改数据库中日期时间的类型
【发布时间】:2011-08-21 22:05:30
【问题描述】:

我正在使用 sql server 数据库并将时间值存储在 datetime 变量中。我正在 vb.net 中开发一个预订系统应用程序。当我想使用 datagridview 查看已经进行的预订并通过实现 dataadapter 和数据集时,它会显示带有系统日期的时间列,该时间列与插入记录时的时间一起保存。现在我想在提取数据时只在时间字段中查看时间....我现在该怎么办??

【问题讨论】:

    标签: vb.net datetime


    【解决方案1】:

    您也可以直接在 datagridview 中使用这样的 dataformatstring 进行格式化:

    <asp:BoundField DataField="TimeStart" HeaderText="Time In" DataFormatString="{0:t}" />
    

    【讨论】:

      【解决方案2】:

      保持它在数据库中的原样。一旦你将数据库中的数据读入 VB 并分配给一个变量,然后做这样的事情

      Dim date1 As Date = #6/1/2008 7:47AM#   // this value being read from the db
      Console.WriteLine(date1.ToString())
      

      使用以下内容:

      ' Get date-only portion of date, without its time.
      Dim dateOnly As Date = date1.Date
      ' Display date using short date string.
      Console.WriteLine(dateOnly.ToString("d"))
      ' Display date using 24-hour clock.
      Console.WriteLine(dateOnly.ToString("g"))
      Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"))   
      ' The example displays the following output to the console:
      '       6/1/2008 7:47:00 AM
      '       6/1/2008
      '       6/1/2008 12:00 AM
      '       06/01/2008 00:00
      

      Dim dtmTest As Date
      dtmTest = DateValue(date1)
      

      【讨论】:

        【解决方案3】:

        DateTime 值以默认格式显示。您应该在网格视图中指定列的格式。

        例子:

        bookingDataGridView.Columns["Created"].DefaultCellStyle.Format = "yyyy-MM-dd";
        

        这些资源应该可以帮助您指定格式,并选择以您想要的方式显示日期的格式字符串:

        How to: Format Data in the Windows Forms DataGridView Control

        Standard Date and Time Format Strings

        Custom Date and Time Format Strings

        【讨论】:

        • 感谢您解决了我的问题
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        • 2011-09-24
        • 2015-03-26
        • 2021-02-20
        相关资源
        最近更新 更多