0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

.NETFrameworkのConvert.ToDateTimeにハマった件

Posted at

多言語対応のWindowsアプリケーションを実装している際に、FormatExceptionではまりました。

DBから日付を取得する際に形式をdd/mm/yyに設定し、String型で取得。
その後Convert.ToDateTimeへその文字列を渡すと、OSの地域、日付形式の設定に依存してFormatExceptionが発生しました。

公式リファレンスを読むと、内部的にDateTime.Parseが呼び出されており、これはOSの地域、日付形式に依存しているので、上記問題が発生しました。
Convert.ToDateTime公式リファレンス

そこで、関数をDateTime.ParseExactへ変更し、形式を指定してあげることで解決しました。

多言語対応の場合は、使う関数がOSの設定に依存しないか確認する必要がありますね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?