- - PR -
文字列をDateTime型に変換
1
投稿者 | 投稿内容 |
---|---|
|
投稿日時: 2003-06-27 13:27
VB.NETでWeb開発をしています。
20030623という文字列があります。 これをDateTime型に変換したいのですが、 うまい方法はありませんか? 今はスラッシュ付の文字列に編集しなおし、それを Parseに渡しています。 文字列編集せずに、もっとうまい方法はありませんか? 20030623 → 2003/06/23 Dim Syonen As DateTime = DateTime.Parse("2003/06/23") |
|
投稿日時: 2003-06-27 16:15
自分のプログラムを見ると、こんなことをやっていました。
DateTime d = DateTime( Convert.ToInt32(dateTime.Substring(0, 4)), Convert.ToInt32(dateTime.Substring(4, 2)), Convert.ToInt32(dateTime.Substring(6, 2))); あまり良くないような...。 |
|
投稿日時: 2003-06-27 17:14
こういうのは
string s = "20030601"; s = Regex.Replace(s, @"(\d{4})(\d{2})(\d{2})", @"$1/$2/$3"); DateTime dt = DateTime.Parse(s); |
|
投稿日時: 2003-06-27 17:57
VB6時代のFormat関数は使えないのですか?
string s = "20030601"; s = Compatibility.VB6.Format(s, "@@@@/@@/@@") (.NET環境が手元に無いため、確認せず。スミマセン...) |
|
投稿日時: 2003-06-27 18:41
こんな感じでしょうか?
System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.DateTimeFormatInfo(); DateTime data = DateTime.ParseExact("20030627", "yyyyMMdd", dtfi); Console.WriteLine(data); |
1