简体   繁体   English

如何使用Oracle日期格式将DateTime转换为C#中的字符串

[英]How to convert DateTime to string in C# using Oracle date format

Goal 目标

Convert a DateTime to string using an Oracle datetime format[1] like "IY-IW". 使用Oracle日期时间格式[1]将DateTime转换为string如“IY-IW”。

Description 描述

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? 如果是,该解决方案将如何?

Notes and references 注释和参考

  1. The Oracle date format specification: http://www.techonthenet.com/oracle/functions/to_char.php Oracle日期格式规范: http//www.techonthenet.com/oracle/functions/to_char.php
  2. Creating a method mapping Oracle datetime formats to C# datetime formats would be possible but I don't consider it being a good or simple solution. 创建将Oracle日期时间格式映射到C#日期时间格式的方法是可能的,但我不认为它是一个好的或简单的解决方案。

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: 特别是:

  • If you only need to support a small number of these formats, it may be worth hard-coding the ones you support 如果您只需要支持少量这些格式,则可能需要对您支持的格式进行硬编码
  • Even if you need to be a bit more flexible than that, I wouldn't try to support everything Oracle supports 即使您需要比这更灵活,我也不会尝试支持Oracle支持的所有内容
  • You may well still run into significant issues around 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通过参数将其作为 DateTimeDateTimeOffset提供。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM