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

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

VB.NET String Data Manipulation for DateTimePicker

vb.netdatetimepicker

提问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/2014DateTime您可以使用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:

如果这是您的本地日期格式,您还可以使用这些更简洁的方法:

  1. )

     Dim date As String = dt.ToShortDateString()
    
  2. )

    Dim date As String = dt.ToString("d")
    
  1. )

     Dim date As String = dt.ToShortDateString()
    
  2. )

    Dim date As String = dt.ToString("d")