[VB.NET] [MYSQL]将字符串转换为datetime时出错,而不是有效格式 [英] [VB.NET] [MYSQL] Error in converting string to datetime and not a valid format

查看:178
本文介绍了[VB.NET] [MYSQL]将字符串转换为datetime时出错,而不是有效格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我遇到了这两种错误。错误1:

转换自字符串   20/05/2015 8:00 AM    类型日期   有效

和错误2:

 字符串 识别 as 一个有效的DateTime 



我的代码下面从MySQL中检索数据类型为'DateTime'的数据

< pre lang =vb> Dim dtstart As DateTime
Dim dtstartdatetime As DateTime
Dim strstartdate As String ' 字符串日期和时间用于在listview中显示为格式化日期
Dim strstarttime As String

< span class =code-keyword>如果 IsDBNull(rdr( info_startdatetime))然后
dtstart = rdr( info_startdatetime
.dtstartdatetime =格式(dtstart, MM / dd / yyyy h:mm tt' 例外情况在这里如果我将笔记本日期格式从M / d / yyyy更改为d / M / yyyy
.strstartdate =格式(dtstart, < span class =code-string> dd-MM M-yyy)。ToString
.strstarttime =格式(dtstart, h:mm tt)。ToString
其他
.dtstartdatetime = 没什么
.strstartdate = 没什么
.strstarttime = 没什么
结束 如果



MySQL的示例数据(默认格式) yyyy-MM-dd Hh:mm:ss):

 2015-05-20 08:00:00 





我的问题:MySQL是否仅以一种格式保存数据?如果是这样,如何在不设置日历设置日期格式的情况下从任何其他计算机检索日期时间?我的意思是普遍转换到日期时间。我试过DateTime.ParseExact,CDate,Convert.ToDateTime。

解决方案

你必须使用 Date.TryParse方法 [ ^ ],它提供了一种使用特定的 culture [ ^ ](本地化)。



您可以使用日期和时间函数 [ ^ ]。



如需了解更多信息,请参阅:全球化和本地化概述 [ ^ ]

http://stackoverflow.com/questions/8338031/mysql-setup-the-format-of-datetime-to- dd-mm-yyyy-hhmmss-when-creating-a-tab [ ^ ]


Hi guys,

I'm experiencing these two types of error. Error 1:

Conversion from String "20/05/2015 8:00AM" is to type Date is not valid

and Error 2:

String was not recognized as a valid DateTime


My code down below retrieve data from MySQL with data type 'DateTime'

Dim dtstart As DateTime
Dim dtstartdatetime As DateTime
Dim strstartdate As String 'Both string date and time use to display in listview as formatted date
Dim strstarttime As String 

If Not IsDBNull(rdr("info_startdatetime")) Then
     dtstart = rdr("info_startdatetime")
     .dtstartdatetime = Format(dtstart, "MM/dd/yyyy h:mm tt")'Exception goes HERE if I change my notebook date format from M/d/yyyy to d/M/yyyy
     .strstartdate = Format(dtstart, "dd-MMM-yyy").ToString
     .strstarttime = Format(dtstart, "h:mm tt").ToString
Else
     .dtstartdatetime = Nothing
     .strstartdate = Nothing
     .strstarttime = Nothing
End If


Sample data on MySQL (default format yyyy-MM-dd Hh:mm:ss) :

2015-05-20 08:00:00



My question: Is MySQL only save data with one format only? If so, how to retrieve datetime from any other computer without setting the date format on calendar settings? I mean universal convertion to datetime. I have tried DateTime.ParseExact, CDate, Convert.ToDateTime.

解决方案

You have to use Date.TryParse method[^], which provides a way to convert date using specific culture[^] (localization).
or
You can convert date while getting data from MySQL server by using Date and Time functions[^].

For further information, please see: Overview of Globalization and Localization[^]
http://stackoverflow.com/questions/8338031/mysql-setup-the-format-of-datetime-to-dd-mm-yyyy-hhmmss-when-creating-a-tab[^]


这篇关于[VB.NET] [MYSQL]将字符串转换为datetime时出错,而不是有效格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆