[英]How to convert DateTime to string in C# using Oracle date format
Convert a DateTime
to string
using an Oracle datetime format[1] like "IY-IW". 使用Oracle日期时间格式[1]将DateTime
转换为string
如“IY-IW”。
I have a situation where I'm provided a DateTime
and an Oracle date format string. 我有一种情况,我提供了DateTime
和Oracle日期格式字符串。 The result should be a string
containing the date and time in the format specified by the Oracle date format. 结果应该是一个string
其中包含Oracle日期格式指定格式的日期和时间。
In short, I would need the method below 简而言之,我需要以下方法
/// <summary>
/// Converts a datetime to a string with the format specified by the Oracle date format.
/// </summary>
/// <param name="oracleDateFormat">
/// Datetime format specified in http://www.techonthenet.com/oracle/functions/to_char.php
/// </param>
/// For example:
/// dateTimeToConvert = 2014-09-23T10:09:47.7739125+02:00, oracleNlsDateFormat = "IY-IW" => result == "14-38"
public string ConvertDateTimeToString(DateTime dateTimeToConvert, string oracleDateFormat);
If done in a database query, I would have used the Oracle to_char method. 如果在数据库查询中完成,我会使用Oracle to_char方法。 However, I need to do the conversion in the C# environment. 但是,我需要在C#环境中进行转换。
The questions above are on the right track but still doesn't give a solution to my problem. 上面的问题是在正确的轨道上,但仍然没有解决我的问题。 For instance, providing DateTime.ParseExact with the format "IY-IW" raises a FormatException. 例如,提供格式为“IY-IW”的DateTime.ParseExact会引发FormatException。
Is there a simple way to achieve the goal? 有没有一种简单的方法来实现目标? Like using DateTime.ParseExact and somehow specifying that an Oracle date format is used? 就像使用DateTime.ParseExact并以某种方式指定使用Oracle日期格式一样? Or converting the Oracle datetime format to C# datetime format on some way[2]? 或者在某种程度上将Oracle日期时间格式转换为C#datetime格式[2]?
If yes, how would that solution look like? 如果是,该解决方案将如何?
Is there a simple way to achieve the goal? 有没有一种简单的方法来实现目标?
Almost certainly not. 几乎肯定不是。
Like using DateTime.ParseExact and somehow specifying that an Oracle date format is used? 就像使用DateTime.ParseExact并以某种方式指定使用Oracle日期格式一样?
No - .NET doesn't know about the Oracle date/time format. 否 - .NET不了解Oracle日期/时间格式。
Converting the Oracle datetime format to C# datetime format on some way? 在某种程度上将Oracle日期时间格式转换为C#datetime格式?
That would be my first suggestion. 这将是我的第一个建议。 In particular: 特别是:
IW
or other week-based formats. 您可能仍会遇到IW
或其他基于周的格式的重大问题。 ( DateTime
doesn't support ISO week-years.) ( DateTime
不支持ISO周 - 年。) I would attempt to remove the requirement in the first place, however. 但是,我会首先尝试删除该要求。 Wherever possible try to avoid encoding dates as strings in the first place - if you're providing a date to the database, provide it as a DateTime
or DateTimeOffset
via a parameter. 尽可能尝试避免首先将日期编码为字符串 - 如果您要为数据库提供DateTimeOffset
通过参数将其作为 DateTime
或DateTimeOffset
提供。 We don't know enough about your context to know whether or not that's an option, but it's worth spending a bit of time trying to remove the requirement if you can. 我们对您的背景知之甚少,不知道这是否是一个选项,但是如果可以的话,花一些时间尝试删除该要求是值得的。
string oracleDateString = dateTimeToConvert.ToString(oracleDateFormat);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.