如何将日期格式从多种格式转换为标准格式 [英] How do I convert a date format from multiple formats to a standard format

查看:136
本文介绍了如何将日期格式从多种格式转换为标准格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI Guys



我目前在生产箱上遇到了一些麻烦。



我的dev和UAT我没有遇到这个问题,但我确实在生产服务器上有这个问题



我正在运行一个导入文件服务,它从FTP位置收集文件导入数据库。



当我导入数据时我收到错误

从字符串转换1/25/2017 7: 59:26 AM键入'日期'无效



我不太确定问题是什么



我尝试过:



目前我从数组中读取数据然后使用以下代码进行转换标准格式的日期

Dim importdate as datetime

ImportedDate = CDate(fields(Count))

Dim FormattedDate As String = ImportedDate。 ToString(dd / MM / yyyy HH:mm:ss)



我也尝试过使用datetime.parse(fields( count).tostring)

HI Guys

I am currently experiencing some trouble on my production box.

On my dev and UAT i have not had this issue but i do have this issue on the production server

I am running a import file service which collects files from an FTP location and imports into the db.

When i import the data i am getting an error
Conversion from string "1/25/2017 7:59:26 AM" to type 'Date' is not valid

I am not too sure what the issue could be

What I have tried:

Currently i am reading the data from an array and then using the following code to convert the date to a standard format
Dim importeddate as datetime
ImportedDate = CDate(fields(Count))
Dim FormattedDate As String = ImportedDate.ToString("dd/MM/yyyy HH:mm:ss")

I have also tried using datetime.parse(fields(count).tostring)

推荐答案

这里有两件事。首先,在阅读日期时,系统将使用您系统的本地格式来解释它。我注意到导入的日期是美国格式(M / dd / yyyy),您输出的是英国格式(dd / MM / yyyy)。如果你的系统使用英国格式,那么在阅读日期大于12的任何美国日期时它会失败,并且在所有其他情况下都会出错。



第二件事是您应该使用DataTime.TryParse方法来解析日期。这将基于成功或失败返回true / false,而不是抛出异常。



因此,我会考虑使用 DateTime.TryParse方法(字符串,IFormatProvider,DateTimeStyles,DateTime)(系统) [ ^ ],允许您指定日期的字符串格式来自的文化,并且不会在失败时抛出异常。
There are two things here. Firstly, when reading a date, the system will use your system's local formats to interpret it. I notice the date being imported is in the US format (M/dd/yyyy), where you are outputting in the British format (dd/MM/yyyy). If your system uses the British format, then it will fail when reading any US date where the day is greater than 12, and will get it wrong in all other cases.

The 2nd thing is that you should probably use the DataTime.TryParse method to parse the date. This will return true/false based on success or failure, rather than throwing an exception.

As such, I would be looking at using the DateTime.TryParse Method (String, IFormatProvider, DateTimeStyles, DateTime) (System)[^], that allows you to specify the culture that the string format of the date comes from, and does not throw an exception on failure.


如果您可以控制日期时间数据的输入,则可以使用 CultureInfo.InvariantCulture Property(System.Globalization) [ ^ ]转换。

了解更多信息来自避免常见.NET中的全球化错误 [ ^

如果没有,您必须知道在进行任何有意义的解析之前您收到的日期时间数据的格式。否则,即使通过,也不会有任何解析可以保证您认为这是正确的日期时间。
If you have control over the input of the datetime data, you can use CultureInfo.InvariantCulture Property (System.Globalization)[^] in the conversion.
Learn more from Avoid common globalization errors in .NET[^].
If not, you have to know the format of the datetime data that you are receiving before any meaningful parsing. Otherwise, no amount of parsing, even if it passes, can promise you that it is the correct datetime that you think you are getting.


这篇关于如何将日期格式从多种格式转换为标准格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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