10

たとえば、1400があるとしましょう。これを、午後2時に変換します。

私は次のことを試しました:

Dim convertedTime As String = DateTime.ParseExact(theTime,"HHmm", Nothing)

そしてそれは私にこれを与えるでしょう:

2012年6月12日02:00:00PM

日付の部分は必要ありません。秒も必要ありません。必要なのは午後2時だけです

どうすればこれを達成できますか?ありがとう!

4

8 に答える 8

17

このParseExactメソッドはDateTime、文字列ではなく値を返します。文字列変数に割り当てると、標準のフォーマットを使用して自動的に変換されます。

特定の形式にする場合は、DateTime値を文字列として形式化します。

Dim d As DateTime = DateTime.ParseExact(theTime,"HHmm", Nothing);
Dim convertedTime As String = d.ToString("hh:mm tt")
于 2012-06-12T21:02:46.260 に答える
2

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日木曜日(または日付が何であれ)

于 2013-06-21T00:41:47.403 に答える
2
Label1.Text = Now.ToShortTimeString.ToString()   (10:26 PM)

Label1.Text = Now.ToLongTimeString.ToString()    (10:26:30 PM) 
于 2013-09-17T06:46:07.930 に答える
1
Dim theTime = New Date(2012, 6, 12, 14, 0, 0)
Dim formatted = theTime.ToString("h:mm tt", Globalization.CultureInfo.InvariantCulture)

カスタムの日付と時刻の形式の文字列

于 2012-06-12T21:05:34.060 に答える
0

これを実現するには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")

どちらの場合convertedTime6:30 AM

于 2012-06-12T21:03:41.393 に答える
0

これを試してください...

  Dim TimeNow As String
  TimeNow = TimeOfDay.ToString("h:mm:ss tt")
于 2014-09-07T15:40:10.050 に答える
0

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

上記のコードは例です。上記のコードを使用して、実験/観察/アプリケーションの作成を行うことができます。

于 2022-02-08T06:02:59.027 に答える
-1
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#の両方の任意のラベルでフォーマットされたタイミングを取得します

于 2022-02-08T06:31:04.010 に答える