如何转换和整数为datetime [英] How do I convert and integer to datetime
问题描述
我使用ASP.NET的Visual Basic。
I am using ASP.NET Visual Basic.
我在我的数据库中的一个整数字段,我需要阅读的日期如20130813.但此刻它说我不能转换为日期时间。
I have an integer field in my database which I need to read as a date e.g. 20130813. But at the moment it says I cannot convert to datetime.
有没有办法分裂整成DD,MM,YYYY所以我甚至可以存储那些变量,然后用它来创建一个日期。或者一个简单的方法只是转换为日期。
Is there a way to split the integer up into DD, MM, YYYY so I could even store those in variables to then use to create a date. Or a simple way of just converting to date.
感谢您的时间。
推荐答案
随着你开始使用一个号码,你既可以把它转换为一个字符串,并使用DateTime.TryParseExact,或使用位整数运算拆分它达:
As you're starting with a number, you could either convert it to a string and use DateTime.TryParseExact, or use a bit of integer arithmetic to split it up:
Dim n As Integer = 20130813
Dim yr As Integer = n \ 10000
Dim mon As Integer = (n - 10000 * yr) \ 100
Dim d As Integer = n Mod 100
Dim dt As New DateTime(yr, mon, d)
这篇关于如何转换和整数为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!