我想操纵我的字符串数据,例如我有一个x =“20140118”到y =“01/18/2014”我该怎么办?我需要它在VB.NET上的DateTimePicker中的值 . 谢谢
DateTimePicker.Value想要一个 DateTime 而不是一个字符串 . 所以你需要解析它:
DateTime
Dim dt As DateTime = DateTime.ParseExact("20140118", "yyyyMMdd", CultureInfo.InvariantCulture) dateTimePicker1.Value = dt
DateTime.ParseExact
Custom Date and Time Format Strings
但是,仅为了完整性,如果您需要 DateTime 中的字符串 01/18/2014 ,您可以使用 DateTime.ToString :
01/18/2014
DateTime.ToString
Dim date As String = dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
如果这是您的本地日期格式,您还可以使用这些更简洁的方法:
Dim date As String = dt.ToShortDateString()
Dim date As String = dt.ToString("d")
1 回答
DateTimePicker.Value想要一个
DateTime
而不是一个字符串 . 所以你需要解析它:DateTime.ParseExact
Custom Date and Time Format Strings
但是,仅为了完整性,如果您需要
DateTime
中的字符串01/18/2014
,您可以使用DateTime.ToString
:如果这是您的本地日期格式,您还可以使用这些更简洁的方法: