.NET/文字列日付をDateTimeに変換する
の編集
Top
/
.NET
/
文字列日付をDateTimeに変換する
[
トップ
] [
編集
|
差分
|
バックアップ
|
添付
|
リロード
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
-- 雛形とするページ --
.NET
.NET/.NETでpingを実現する・System.Net.NetworkInformation.Ping
.NET/ARPテーブルを取得し表示するサンプル・GetIpNetTable
.NET/CPUコア毎の使用率を取得する
.NET/CPU使用率を取得する
.NET/DateとDateTimeの違い
.NET/HttpClientを使って指定したURLのページを取得する
.NET/IPv4からMACアドレス取得する・SendARP
.NET/IPアドレスからホスト名を取得するサンプルコード
.NET/ListのStringをカンマ区切りにする方法
.NET/Listを配列に配列をListに変換する
.NET/NET6.0でSQLiteを使ってみた Microsoft.Data.Sqlite
.NET/ODBCを使用してデータベースに接続しSQLを実行するサンプル
.NET/String.Splitで空要素を残す、空要素を削除するオプションについて
.NET/StringBuilderをバイト配列に変換する
.NET/Stringの配列をCSVにする方法
.NET/UNIX時間への変換・DateTimeOffset
.NET/UNIX時間をDateTimeOffset, DateTimeにする
.NET/Web APIからJSONを取得し値を表示する・DataContractJsonSerializer
.NET/WMIを使って物理メモリ・仮想メモリの使用量等を取得する
.NET/アルファベットを大文字、小文字に変換する
.NET/インストールされているODBCドライバ一覧を取得するサンプル
.NET/コンソールプログラムからトースト通知をするサンプル
.NET/サスペンド前・復帰の時に処理を行いたい
.NET/スペースのみの文字列、空の文字列、サイズ0の文字列を判定する
.NET/スリープの抑止、モニター電源オフの抑止のサンプルコード
.NET/ターゲットにしている.NETを確認する操作手順
.NET/テキストファイルの読込・ReadAllText,ReadAllLines
.NET/テキストファイルの読込・StreamReader
.NET/テンポラリファイルの作成・ランダムな文字列の作成を作成する
.NET/テンポラリファイル名を取得する・GetRandomFileName
.NET/ディレクトリの有無を確認する
.NET/ネットワークカードの各種情報を取得するサンプルコード
.NET/バイト配列にファイルの内容をすべて読み込む
.NET/バイナリーファイルの作成・リード・ライト
.NET/ファイルなの?ディレクトリなの?の判別
.NET/ファイルの有無を確認する
.NET/ファイル一覧を取得する・GetFiles,EnumerateFiles
.NET/プログラムに渡された引数を取得する方法
.NET/ユーザーが無操作の状態を取得する・GetLastInputInfo
.NET/三項演算子のサンプルコード
.NET/四捨五入をするときはMidpointRounding.AwayFromZeroにすること
.NET/指定した時間間隔でイベントを発行する・Timer
.NET/数値を指定した桁数分ゼロで埋めたい・ToString
.NET/文字列(String)を左右逆転し文字列(String)で返却する方法
.NET/文字列をバイト配列に変換・バイト配列を文字列に変換
.NET/文字列を区切り文字で分割する方法・Split
.NET/文字列宣言の先頭の@について
.NET/文字列数値を指定した桁数分ゼロで埋めたい・PadLeft
.NET/文字列日付をDateTimeに変換する
.NET/文字列配列orList配列に指定した文字列が存在するかを確認する方法
.NET/時間(日数+時間)が格納できるTimeSpan
.NET/月末の日を取得する方法・DaysInMonth
.NET/現在の日時を取得する
.NET/符号付バイトsbyte,符号なしbyteをintegerに変換する
.NET/管理者として実行されているかどうか確認するサンプルコード
.NET/連想配列(ハッシュ)のキーと値を取得する
.NET/連想配列(ハッシュ)を使ってみる・Dictionary
.NET/閏年かを調べる方法
AutoTicketLinkName
FrontPage
MenuBar
OSS
OSS/XYM日本円価格(シンボル日本円価格 (XYMJPYPrice)
OSS/ビットコイン日本円価格(ビットコイン日本円価格 (BitcoinJPYPrice)
RecentDeleted
VS
VS/Debug.Writeをイミディエイトウィンドウに出力する方法
VS/Visual Studio 2022に Spy++をインストールする手順
VS/VS2022の配色テーマの変更操作
VS/XAML編集に使うツールボックスの再表示する操作手順
VS/インデントなどコードを整形するショートカットの紹介
VS/コメントにするショートカットキー
VS/ショートカットキーで大文字・小文字に変換する
VS/ビルド中に中断したい場合の操作手順
WPF
WPF/DataGridを使ってみる
WPF/起動してもMainWindowを表示したくない場合
お約束
プライバシーポリシー
最初にお読みください
#navi(../) * 文字列日付をDateTimeに変換する [#t6855c5b] 文字列日時をDateTime型に変換するメソッド ''DateTime.ParseExact'' と ''DateTime.TryParseExact'' の使用例を紹介します。~ #contents * 関連サイト [#b8ad5706] -[[Microsoft|.NET DateTime.ParseExact メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.datetime.tryparseexact?view=netframework-4.8]] -[[Microsoft|.NET DateTime.TryParseExact メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.datetime.tryparseexact?view=netframework-4.8]] -[[Microsoft|.NET DateTimeStyles 列挙型>https://docs.microsoft.com/ja-jp/dotnet/api/system.globalization.datetimestyles?view=netframework-4.8]] -[[Microsoft|.NET カスタム日時形式文字列>https://docs.microsoft.com/ja-jp/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netframework-4.8]] #htmlinsert(windev-top.html) * 関連記事 [#f9c23961] -[[DateとDateTimeの違い>.NET/DateとDateTimeの違い]] * 日時文字列の変換フォーマット [#x28dfef1] 以下によく使うと思われる、年月日時分秒を指定する日時形式文字列について記します。~ 詳細な情報は、以下のリンクを参照ください。 -[[Microsoft|.NET カスタム日時形式文字列>https://docs.microsoft.com/ja-jp/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netframework-4.8]] |BGCOLOR(NAVY):COLOR(WHITE):''日時形式文字列''|BGCOLOR(NAVY):COLOR(WHITE):''説明''| |yyyy|年の4桁| |MM|月の2桁| |dd|日の2桁| |hh|時間の2桁(24時間)| |mm|分の2桁| |ss|秒の2桁| * ''DateTime.ParseExact'' と ''DateTime.TryParseExact'' の違い [#c49a70ef] DateTime.ParseExactとDateTime.TryParseExactの違いは、変換失敗時に例外を発生するか、日時変換成功/失敗時にTrue/Falseを返却するかの違いになります。 |BGCOLOR(NAVY):COLOR(WHITE):''メソッド''|BGCOLOR(NAVY):COLOR(WHITE):''説明''| |DateTime.ParseExact|変換失敗時は例外が発生します。| |DateTime.TryParseExact|変換成功時は、True, 失敗時はFalseを返却します。&br;成功時には引数で渡したDateTimeに変換した値が格納されます。| * サンプルコード [#ab8cf070] C#とVisual Basic(VB)のサンプルコードを紹介します。 ** C#のサンプルコード [#l2bb9f9d] using System; using System.Globalization; class Program { static void Main(string[] args) { String dt1str = @"2019/12/10 10:20:30"; String dt2str = @"2019/12/10 10:20:90"; String fmt = @"yyyy/MM/dd hh:mm:ss"; DateTime dt; // ParseExactのサンプル try { dt = DateTime.ParseExact(dt1str, fmt, null); Console.WriteLine("dt1str:" + dt); } catch (Exception ex) { Console.WriteLine("Exception:" + ex.Message); } try { dt = DateTime.ParseExact(dt2str, fmt, null); Console.WriteLine("dt2str:" + dt); } catch (Exception ex) { Console.WriteLine("Exception:" + ex.Message); } // TryParseExactのサンプル Boolean b; b = DateTime.TryParseExact(dt1str, fmt, null, DateTimeStyles.AssumeLocal, out dt); Console.WriteLine("dt1str:" + b); b = DateTime.TryParseExact(dt2str, fmt, null, DateTimeStyles.AssumeLocal, out dt); Console.WriteLine("dt2str:" + b); } } ** Visual Basic(VB)のサンプルコード [#z6251459] Imports System Imports System.Globalization Module Program Sub Main(args As String()) Dim dt1str As String = "2019/12/10 10:20:30" Dim dt2str As String = "2019/12/10 10:20:90" Dim fmt As String = "yyyy/MM/dd hh:mm:ss" Dim dt As DateTime ' ParseExactのサンプル Try dt = DateTime.ParseExact(dt1str, fmt, Nothing) Console.WriteLine("dt1str:" & dt) Catch ex As Exception Console.WriteLine("Exception:" & ex.Message) End Try Try dt = DateTime.ParseExact(dt2str, fmt, Nothing) Console.WriteLine("dt2str:" & dt) Catch ex As Exception Console.WriteLine("Exception:" & ex.Message) End Try ' TryParseExactのサンプル Dim b As Boolean b = DateTime.TryParseExact(dt1str, fmt, Nothing, DateTimeStyles.AssumeLocal, dt) Console.WriteLine("dt1str:" & b) b = DateTime.TryParseExact(dt2str, fmt, Nothing, DateTimeStyles.AssumeLocal, dt) Console.WriteLine("dt2str:" & b) End Sub End Module ** 実行結果 [#eedb8bcb] 実行結果は以下のようになります。~ dt2strの秒が90となっているため、DateTime.ParseExactでは例外が発生し、DateTime.TryParseExactではfalseが返却されます。~ #ref(01.png) ** その他 [#k8b514cc] DateTime.TryParseExactメソッドでは、引数に''DateTimeStyles.AssumeLocal''を渡しています。~ この説明については、以下のリンクを参照ください。 -[[Microsoft|.NET DateTimeStyles 列挙型>https://docs.microsoft.com/ja-jp/dotnet/api/system.globalization.datetimestyles?view=netframework-4.8]] 以上、文字列日時をDateTimeに変換するメソッドとサンプルコードの紹介でした。 #htmlinsert(windev-btm.html)
タイムスタンプを変更しない
#navi(../) * 文字列日付をDateTimeに変換する [#t6855c5b] 文字列日時をDateTime型に変換するメソッド ''DateTime.ParseExact'' と ''DateTime.TryParseExact'' の使用例を紹介します。~ #contents * 関連サイト [#b8ad5706] -[[Microsoft|.NET DateTime.ParseExact メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.datetime.tryparseexact?view=netframework-4.8]] -[[Microsoft|.NET DateTime.TryParseExact メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.datetime.tryparseexact?view=netframework-4.8]] -[[Microsoft|.NET DateTimeStyles 列挙型>https://docs.microsoft.com/ja-jp/dotnet/api/system.globalization.datetimestyles?view=netframework-4.8]] -[[Microsoft|.NET カスタム日時形式文字列>https://docs.microsoft.com/ja-jp/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netframework-4.8]] #htmlinsert(windev-top.html) * 関連記事 [#f9c23961] -[[DateとDateTimeの違い>.NET/DateとDateTimeの違い]] * 日時文字列の変換フォーマット [#x28dfef1] 以下によく使うと思われる、年月日時分秒を指定する日時形式文字列について記します。~ 詳細な情報は、以下のリンクを参照ください。 -[[Microsoft|.NET カスタム日時形式文字列>https://docs.microsoft.com/ja-jp/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netframework-4.8]] |BGCOLOR(NAVY):COLOR(WHITE):''日時形式文字列''|BGCOLOR(NAVY):COLOR(WHITE):''説明''| |yyyy|年の4桁| |MM|月の2桁| |dd|日の2桁| |hh|時間の2桁(24時間)| |mm|分の2桁| |ss|秒の2桁| * ''DateTime.ParseExact'' と ''DateTime.TryParseExact'' の違い [#c49a70ef] DateTime.ParseExactとDateTime.TryParseExactの違いは、変換失敗時に例外を発生するか、日時変換成功/失敗時にTrue/Falseを返却するかの違いになります。 |BGCOLOR(NAVY):COLOR(WHITE):''メソッド''|BGCOLOR(NAVY):COLOR(WHITE):''説明''| |DateTime.ParseExact|変換失敗時は例外が発生します。| |DateTime.TryParseExact|変換成功時は、True, 失敗時はFalseを返却します。&br;成功時には引数で渡したDateTimeに変換した値が格納されます。| * サンプルコード [#ab8cf070] C#とVisual Basic(VB)のサンプルコードを紹介します。 ** C#のサンプルコード [#l2bb9f9d] using System; using System.Globalization; class Program { static void Main(string[] args) { String dt1str = @"2019/12/10 10:20:30"; String dt2str = @"2019/12/10 10:20:90"; String fmt = @"yyyy/MM/dd hh:mm:ss"; DateTime dt; // ParseExactのサンプル try { dt = DateTime.ParseExact(dt1str, fmt, null); Console.WriteLine("dt1str:" + dt); } catch (Exception ex) { Console.WriteLine("Exception:" + ex.Message); } try { dt = DateTime.ParseExact(dt2str, fmt, null); Console.WriteLine("dt2str:" + dt); } catch (Exception ex) { Console.WriteLine("Exception:" + ex.Message); } // TryParseExactのサンプル Boolean b; b = DateTime.TryParseExact(dt1str, fmt, null, DateTimeStyles.AssumeLocal, out dt); Console.WriteLine("dt1str:" + b); b = DateTime.TryParseExact(dt2str, fmt, null, DateTimeStyles.AssumeLocal, out dt); Console.WriteLine("dt2str:" + b); } } ** Visual Basic(VB)のサンプルコード [#z6251459] Imports System Imports System.Globalization Module Program Sub Main(args As String()) Dim dt1str As String = "2019/12/10 10:20:30" Dim dt2str As String = "2019/12/10 10:20:90" Dim fmt As String = "yyyy/MM/dd hh:mm:ss" Dim dt As DateTime ' ParseExactのサンプル Try dt = DateTime.ParseExact(dt1str, fmt, Nothing) Console.WriteLine("dt1str:" & dt) Catch ex As Exception Console.WriteLine("Exception:" & ex.Message) End Try Try dt = DateTime.ParseExact(dt2str, fmt, Nothing) Console.WriteLine("dt2str:" & dt) Catch ex As Exception Console.WriteLine("Exception:" & ex.Message) End Try ' TryParseExactのサンプル Dim b As Boolean b = DateTime.TryParseExact(dt1str, fmt, Nothing, DateTimeStyles.AssumeLocal, dt) Console.WriteLine("dt1str:" & b) b = DateTime.TryParseExact(dt2str, fmt, Nothing, DateTimeStyles.AssumeLocal, dt) Console.WriteLine("dt2str:" & b) End Sub End Module ** 実行結果 [#eedb8bcb] 実行結果は以下のようになります。~ dt2strの秒が90となっているため、DateTime.ParseExactでは例外が発生し、DateTime.TryParseExactではfalseが返却されます。~ #ref(01.png) ** その他 [#k8b514cc] DateTime.TryParseExactメソッドでは、引数に''DateTimeStyles.AssumeLocal''を渡しています。~ この説明については、以下のリンクを参照ください。 -[[Microsoft|.NET DateTimeStyles 列挙型>https://docs.microsoft.com/ja-jp/dotnet/api/system.globalization.datetimestyles?view=netframework-4.8]] 以上、文字列日時をDateTimeに変換するメソッドとサンプルコードの紹介でした。 #htmlinsert(windev-btm.html)
テキスト整形のルールを表示する
添付ファイル:
01.png
224件
[
詳細
]