たとえば、1400があるとしましょう。これを、午後2時に変換します。
私は次のことを試しました:
Dim convertedTime As String = DateTime.ParseExact(theTime,"HHmm", Nothing)
そしてそれは私にこれを与えるでしょう:
2012年6月12日02:00:00PM
日付の部分は必要ありません。秒も必要ありません。必要なのは午後2時だけです
どうすればこれを達成できますか?ありがとう!
たとえば、1400があるとしましょう。これを、午後2時に変換します。
私は次のことを試しました:
Dim convertedTime As String = DateTime.ParseExact(theTime,"HHmm", Nothing)
そしてそれは私にこれを与えるでしょう:
2012年6月12日02:00:00PM
日付の部分は必要ありません。秒も必要ありません。必要なのは午後2時だけです
どうすればこれを達成できますか?ありがとう!
このParseExact
メソッドはDateTime
、文字列ではなく値を返します。文字列変数に割り当てると、標準のフォーマットを使用して自動的に変換されます。
特定の形式にする場合は、DateTime
値を文字列として形式化します。
Dim d As DateTime = DateTime.ParseExact(theTime,"HHmm", Nothing);
Dim convertedTime As String = d.ToString("hh:mm tt")
Label1.Text = Format(Now, "hh:mm")
:Label1のテキスト= 10:26(または任意の時間)
Label1.Text = Format(Now, "hh:mm tt")
:ラベルのテキスト= 10:26 PM
Label1.Text = Format(Now, "dddd dd, MMMM, YYYY")
:Label1のテキスト= 2014年8月21日木曜日(または日付が何であれ)
Label1.Text = Now.ToShortTimeString.ToString() (10:26 PM)
Label1.Text = Now.ToLongTimeString.ToString() (10:26:30 PM)
Dim theTime = New Date(2012, 6, 12, 14, 0, 0)
Dim formatted = theTime.ToString("h:mm tt", Globalization.CultureInfo.InvariantCulture)
これを実現するには2つの方法があります。
オプション1(標準の日付と時刻の形式の文字列を使用):
Dim theTime As DateTime = new DateTime(2008, 4, 10, 6, 30, 0)
Dim convertedTime As String =
theTime.ToString("t", CultureInfo.CreateSpecificCulture("en-us"))
オプション2(カスタムの日付と時刻の形式の文字列を使用):
Dim theTime As DateTime = new DateTime(2008, 4, 10, 6, 30, 0)
Dim convertedTime As String = theTime.ToString("hh:mm tt")
どちらの場合convertedTime
も6:30 AM
これを試してください...
Dim TimeNow As String
TimeNow = TimeOfDay.ToString("h:mm:ss tt")
String.Format()
機能なしでParseExtract()
機能を使用できます
VB.NET:-
Dim time As String = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) '12:00 PM'
Dim time_1 As String = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", Date.Now) '12:00:25 PM'
Dim time_2 As String = String.Format("{0:mm}:{0:ss}.{0:fff}", Date.Now) '00:25.986 - Mostly Used In Stopwatches'
CSharp(C#):-
String time = String.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // 12:00 PM
String time_1 = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", DateTime.Now); // 12:00:25 PM
String time_2 = String.Format("{0:mm}:{0:ss}.{0:fff}", DateTime.Now); // 00:25.986 - Mostly Used In Stopwatches
上記のコードは例です。上記のコードを使用して、実験/観察/アプリケーションの作成を行うことができます。
stackoverflowuser:
You Can Use String.Format() Function Without ParseExtract() Function
VB.NET:-
Dim time As String = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) '12:00 PM'
Dim time_1 As String = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", Date.Now) '12:00:25 PM'
Dim time_2 As String = String.Format("{0:mm}:{0:ss}.{0:fff}", Date.Now) '00:25.986 - Mostly Used In Stopwatches'
CSharp (C#):-
String time = String.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // 12:00 PM
String time_1 = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", DateTime.Now); // 12:00:25 PM
String time_2 = String.Format("{0:mm}:{0:ss}.{0:fff}", DateTime.Now); // 00:25.986 - Mostly Used In Stopwatches
Above Codes Are Examples, You Can Use Above Codes To Experiment/Observe/Make Applications With It
次の場所でも使用できます:
VB.NET:-
Label1.Text = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) 'Display: 12:00 PM'
CSharp(C#):-
label1.Text = SString.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // Display: 12:00 PM
VB.NETとC#の両方の任意のラベルでフォーマットされたタイミングを取得します