vb.net 如何将整数转换为日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34063545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:36:55  来源:igfitidea点击:

How to Convert Integer to Date?

vb.netvisual-studio-2012

提问by Chad Patrick

Is it possible to convert integer to date? I am using a datagridview where users can type a combinations of number (e.g 07191993) when in editmodethen after the user is done editing, the program should format it in date (07-19-1993).

是否可以将整数转换为日期?我正在使用数据网格视图,用户可以在其中输入数字组合(例如 07191993),editmode然后在用户完成编辑后,程序应将其格式化为日期 (07-19-1993)。

回答by Sehnsucht

To get a date from the number inputed by user :

从用户输入的数字中获取日期:

Dim theDate = DateTime.ParseExact (input, "MMddyyyy", CultureInfo.InvariantCulture) ' or some other specific CultureInfo / FormatProvider)

To display the date as wanted (if given FormatProvider doesn't do it by itself) :

根据需要显示日期(如果给定的 FormatProvider 本身不显示):

Dim repr = theDate.ToString ("MM-dd-yyyy")

回答by akhil kumar

Create an event for CellEndEditof datagridviewand put in this code

CellEndEditof创建一个事件datagridview并放入此代码

    Dim n As Integer = 'Datagridview cell value'
    Dim mon As Integer = Convert.ToInt32(n.ToString.Substring(0, 1))
    Dim dt As Integer = Convert.ToInt32(n.ToString.Substring(1, 2))
    Dim yr As Integer = Convert.ToInt32(n.ToString.Substring(3, 4))
    Dim dt1 As New DateTime(yr, mon, dt)

hope this helps.

希望这可以帮助。