简体   繁体   English

将字符串格式化为数据网格中的日期时间 vb.net

[英]format string to a datetime in a datagrid vb.net

    dg_datos.Columns.Add("tarea", "Tarea")
    dg_datos.Columns.Add("horas", "Horas")
    dg_datos.Columns.Add("descripcion", "Descripcion")
    dg_datos.Columns.Add("fecha", "Fecha")
    dg_datos.Columns("fecha").DefaultCellStyle.Format = "dd/MM/yyyy"


    dg_datos.Rows.Add("Ada Lovelace", "1", "desc.1", "10/11/2002")

I think I did not correctly specify the date format我想我没有正确指定日期格式

You don't format a String , as I already said.正如我已经说过的,您不需要格式化String Just as numeric format specifiers only work on numbers, date format specifiers only work on Dates .正如数字格式说明符仅适用于数字,日期格式说明符仅适用于Dates The immediate fix is to use an actual Date :直接的解决方法是使用实际的Date

dg_datos.Rows.Add("Ada Lovelace", "1", "desc.1", #11/10/2002#)

That is using a Date literal.那是使用Date文字。 Note that Date literals are ALWAYS in the format #M/dd/yyyy# .请注意, Date文字始终采用#M/dd/yyyy#格式。 So I have switched the day and month values in your example.所以我在你的例子中切换了日期和月份的值。 Now that you have an actual Date , date format specifiers will actually work.现在您有了一个实际的Date ,日期格式说明符将真正起作用。

You can get a Date value in various different ways.您可以通过各种不同的方式获取Date值。 In places where you would use literals for Strings or numbers, you can use a Date literal too.在将文字用于Strings或数字的地方,您也可以使用Date文字。 If you want to construct a Date though, there are numerous ways.如果你想构造一个Date ,有很多方法。 For example, you might use Date.Now for the current date and time, Date.Today to get the current date with the time zeroed.例如,您可以使用Date.Now获取当前日期和时间,使用Date.Today获取时间归零的当前日期。 You can also use a constructor to build a Date from individual numbers for each component.您还可以使用构造函数从每个组件的单独数字构建Date

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

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