vb.net - 从字符串转换为日期的日期格式是什么
问题描述
我有下面的代码来获取 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)
解决方案
你可以使用 DateTime.TryParse
Dim MyDate As DateTime = Nothing
Dim strNewDate As String = "04/29/2019"
DateTime.TryParse(strNewDate, MyDate)
编辑(误读您的问题)- DateTime 持有价值而不是字符串。要在解析日期后获得您所要求的内容,您需要使用格式方法将其转换回字符串或使用以下内容:
MyDate.ToShortDateString
推荐阅读
- azure-devops - 在 Azure Pipeline 中找不到已发布的工件
- python - 在另一个函数中使用创建的类时出现 Python 错误
- sql - 使用多个计数/总和跨多个字段进行透视
- php - 如果在 MySQL 中满足条件,则同时执行 SELECT 和 UPDATE
- c++ - 将数组中的离散 i 值分配给具有离散名称的变量 (C++)
- visual-studio-code - 不允许 Visual Studio 代码 EPERM 操作
- c# - 错误 NU1101:找不到包“”。源中不存在具有此 ID 的包:Microsoft Visual Studio 脱机包,nuget.org
- c++ - 无法使用 SDK 将人脸数据上传到海康威视摄像头
- openid-connect - 为 OpenID Connect RP 跳过 ADFS 中的 Home Realm Discovery 页面
- python - 为什么 PyTorch nn.Module.cuda() 不移动模块张量而只移动参数和缓冲区到 GPU?