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
How to Convert Integer to Date?
提问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.
希望这可以帮助。

