.NET/文字列日付をDateTimeに変換する
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* 文字列日付をDateTimeに変換する [#t6855c5b]
文字列日時をDateTime型に変換するメソッド ''DateTime.Parse...
#contents
* 関連サイト [#b8ad5706]
-[[Microsoft|.NET DateTime.ParseExact メソッド>https://do...
-[[Microsoft|.NET DateTime.TryParseExact メソッド>https:/...
-[[Microsoft|.NET DateTimeStyles 列挙型>https://docs.micr...
-[[Microsoft|.NET カスタム日時形式文字列>https://docs.mic...
#htmlinsert(windev-top.html)
* 関連記事 [#f9c23961]
-[[DateとDateTimeの違い>.NET/DateとDateTimeの違い]]
* 日時文字列の変換フォーマット [#x28dfef1]
以下によく使うと思われる、年月日時分秒を指定する日時形式...
詳細な情報は、以下のリンクを参照ください。
-[[Microsoft|.NET カスタム日時形式文字列>https://docs.mic...
|BGCOLOR(NAVY):COLOR(WHITE):''日時形式文字列''|BGCOLOR(NA...
|yyyy|年の4桁|
|MM|月の2桁|
|dd|日の2桁|
|hh|時間の2桁(24時間)|
|mm|分の2桁|
|ss|秒の2桁|
* ''DateTime.ParseExact'' と ''DateTime.TryParseExact'' ...
DateTime.ParseExactとDateTime.TryParseExactの違いは、変換...
|BGCOLOR(NAVY):COLOR(WHITE):''メソッド''|BGCOLOR(NAVY):CO...
|DateTime.ParseExact|変換失敗時は例外が発生します。|
|DateTime.TryParseExact|変換成功時は、True, 失敗時はFalse...
* サンプルコード [#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, Da...
Console.WriteLine("dt1str:" + b);
b = DateTime.TryParseExact(dt2str, fmt, null, Da...
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,...
Console.WriteLine("dt1str:" & b)
b = DateTime.TryParseExact(dt2str, fmt, Nothing,...
Console.WriteLine("dt2str:" & b)
End Sub
End Module
** 実行結果 [#eedb8bcb]
実行結果は以下のようになります。~
dt2strの秒が90となっているため、DateTime.ParseExactでは例...
#ref(01.png)
** その他 [#k8b514cc]
DateTime.TryParseExactメソッドでは、引数に''DateTimeStyle...
この説明については、以下のリンクを参照ください。
-[[Microsoft|.NET DateTimeStyles 列挙型>https://docs.micr...
以上、文字列日時をDateTimeに変換するメソッドとサンプルコ...
#htmlinsert(windev-btm.html)
終了行:
#navi(../)
* 文字列日付をDateTimeに変換する [#t6855c5b]
文字列日時をDateTime型に変換するメソッド ''DateTime.Parse...
#contents
* 関連サイト [#b8ad5706]
-[[Microsoft|.NET DateTime.ParseExact メソッド>https://do...
-[[Microsoft|.NET DateTime.TryParseExact メソッド>https:/...
-[[Microsoft|.NET DateTimeStyles 列挙型>https://docs.micr...
-[[Microsoft|.NET カスタム日時形式文字列>https://docs.mic...
#htmlinsert(windev-top.html)
* 関連記事 [#f9c23961]
-[[DateとDateTimeの違い>.NET/DateとDateTimeの違い]]
* 日時文字列の変換フォーマット [#x28dfef1]
以下によく使うと思われる、年月日時分秒を指定する日時形式...
詳細な情報は、以下のリンクを参照ください。
-[[Microsoft|.NET カスタム日時形式文字列>https://docs.mic...
|BGCOLOR(NAVY):COLOR(WHITE):''日時形式文字列''|BGCOLOR(NA...
|yyyy|年の4桁|
|MM|月の2桁|
|dd|日の2桁|
|hh|時間の2桁(24時間)|
|mm|分の2桁|
|ss|秒の2桁|
* ''DateTime.ParseExact'' と ''DateTime.TryParseExact'' ...
DateTime.ParseExactとDateTime.TryParseExactの違いは、変換...
|BGCOLOR(NAVY):COLOR(WHITE):''メソッド''|BGCOLOR(NAVY):CO...
|DateTime.ParseExact|変換失敗時は例外が発生します。|
|DateTime.TryParseExact|変換成功時は、True, 失敗時はFalse...
* サンプルコード [#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, Da...
Console.WriteLine("dt1str:" + b);
b = DateTime.TryParseExact(dt2str, fmt, null, Da...
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,...
Console.WriteLine("dt1str:" & b)
b = DateTime.TryParseExact(dt2str, fmt, Nothing,...
Console.WriteLine("dt2str:" & b)
End Sub
End Module
** 実行結果 [#eedb8bcb]
実行結果は以下のようになります。~
dt2strの秒が90となっているため、DateTime.ParseExactでは例...
#ref(01.png)
** その他 [#k8b514cc]
DateTime.TryParseExactメソッドでは、引数に''DateTimeStyle...
この説明については、以下のリンクを参照ください。
-[[Microsoft|.NET DateTimeStyles 列挙型>https://docs.micr...
以上、文字列日時をDateTimeに変換するメソッドとサンプルコ...
#htmlinsert(windev-btm.html)
ページ名: