首页 > 解决方案 > 从字符串转换为日期的日期格式是什么

问题描述

我有下面的代码来获取 StringToDate 为 04/29/2019,但是当使用 CDate 函数时,它给了我 #4/29/2019 12:00:00 AM# 值。我必须使用哪种日期格式才能获得 2019 年 4 月 29 日

    Dim StringToDate As Date
    Dim strNewDate As String = "04/29/2019"
    StringToDate = CDate(strNewDate)

标签: vb.net

解决方案


你可以使用 DateTime.TryParse

    Dim MyDate As DateTime = Nothing
    Dim strNewDate As String = "04/29/2019"
    DateTime.TryParse(strNewDate, MyDate)

编辑(误读您的问题)- DateTime 持有价值而不是字符串。要在解析日期后获得您所要求的内容,您需要使用格式方法将其转换回字符串或使用以下内容:

MyDate.ToShortDateString

推荐阅读