DateTimePicker 的 VB.NET 字符串数据操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25199129/
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
VB.NET String Data Manipulation for DateTimePicker
提问by user3921411
I want to manipulate my String data for example I have a x="20140118" to y="01/18/2014" how can I do it? I need it for the value in DateTimePicker on VB.NET. Thanks
我想操作我的字符串数据,例如我有 ax="20140118" 到 y="01/18/2014" 我该怎么做?我需要它作为 VB.NET 上 DateTimePicker 中的值。谢谢
回答by Tim Schmelter
DateTimePicker.Valuewants a DateTimenot a string. So you need to parse it:
DateTimePicker.Value想要一个DateTime不是字符串。所以你需要解析它:
Dim dt As DateTime = DateTime.ParseExact("20140118", "yyyyMMdd", CultureInfo.InvariantCulture)
dateTimePicker1.Value = dt
However, just for the sake of completeness, if you need a string 01/18/2014from the DateTimeyou can use DateTime.ToString:
但是,为了完整起见,如果您需要一个字符串01/18/2014,DateTime您可以使用DateTime.ToString:
Dim date As String = dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
If that is your local date format you could also use these more concise approaches:
如果这是您的本地日期格式,您还可以使用这些更简洁的方法:
)
Dim date As String = dt.ToShortDateString())
Dim date As String = dt.ToString("d")
)
Dim date As String = dt.ToShortDateString())
Dim date As String = dt.ToString("d")

