Vb.net-date-time
VB.Net-日付と時刻
作成するソフトウェアのほとんどは、現在の日付と時刻を返す何らかの形式の日付関数を実装する必要があります。 日付は日常生活の非常に重要な部分であるため、考えずに日付を使用することは簡単です。 VB.Netは、日付の操作を簡単にする日付計算用の強力なツールも提供します。
DateAndTimeクラスから現在の日付と時刻を取得することもできます。
a |
DateTime構造とDateAndTimeモジュールの両方には、 Now や Today などのプロパティが含まれているため、初心者は混乱を招くことがよくあります。 DateAndTimeクラスはMicrosoft.VisualBasic名前空間に属し、DateTime構造体はSystem名前空間に属します。 したがって、後者を使用すると、C#などの別の.Net言語にコードを移植するときに役立ちます。 ただし、DateAndTimeクラス/モジュールには、Visual Basicで使用可能なすべてのレガシー日付関数が含まれています。 |
DateTime構造のプロパティとメソッド
次の表は、 DateTime 構造の一般的に使用される*プロパティ*の一部を示しています-
Sr.No | Property | Description |
---|---|---|
1 | Date | Gets the date component of this instance. |
2 | Day | Gets the day of the month represented by this instance. |
3 | DayOfWeek | Gets the day of the week represented by this instance. |
4 | DayOfYear | Gets the day of the year represented by this instance. |
5 | Hour | Gets the hour component of the date represented by this instance. |
6 | Kind | Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither. |
7 | Millisecond | Gets the milliseconds component of the date represented by this instance. |
8 | Minute | Gets the minute component of the date represented by this instance. |
9 | Month | Gets the month component of the date represented by this instance. |
10 | *Now * | Gets a* DateTime* object that is set to the current date and time on this computer, expressed as the local time. |
11 | Second | Gets the seconds component of the date represented by this instance. |
12 | Ticks | Gets the number of ticks that represent the date and time of this instance. |
13 | TimeOfDay | Gets the time of day for this instance. |
14 | Today | Gets the current date. |
15 | *UtcNow * | Gets a* DateTime* object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC). |
16 | Year | Gets the year component of the date represented by this instance. |
次の表は、一般的に使用される DateTime 構造体の*メソッド*の一部です-
Sr.No | Method Name & Description |
---|---|
1 |
Public Function Add (value As TimeSpan) As DateTime 指定したTimeSpanの値をこのインスタンスの値に追加する新しいDateTimeを返します。 |
2 |
Public Function AddDays ( value As Double) As DateTime 指定した日数をこのインスタンスの値に追加する新しいDateTimeを返します。 |
3 |
Public Function AddHours (value As Double) As DateTime 指定した時間数をこのインスタンスの値に追加する新しいDateTimeを返します。 |
4 |
Public Function AddMinutes (value As Double) As DateTime 指定した分数をこのインスタンスの値に追加する新しいDateTimeを返します。 |
5 |
Public Function AddMonths (months As Integer) As DateTime 指定した月数をこのインスタンスの値に追加する新しいDateTimeを返します。 |
6 |
Public Function AddSeconds (value As Double) As DateTime 指定した秒数をこのインスタンスの値に追加する新しいDateTimeを返します。 |
7 |
Public Function AddYears (value As Integer ) As DateTime 指定した年数をこのインスタンスの値に追加する新しいDateTimeを返します。 |
8 |
Public Shared Function Compare (t1 As DateTime,t2 As DateTime) As Integer DateTimeの2つのインスタンスを比較し、最初のインスタンスが2番目のインスタンスより早いか、同じか、遅いかを示す整数を返します。 |
9 |
Public Function CompareTo (value As DateTime) As Integer このインスタンスの値を指定されたDateTime値と比較し、このインスタンスが指定されたDateTime値より早いか、同じか、遅いかを示す整数を返します。 |
10 |
Public Function Equals (value As DateTime) As Boolean このインスタンスの値が指定されたDateTimeインスタンスの値と等しいかどうかを示す値を返します。 |
11 |
Public Shared Function Equals (t1 As DateTime, t2 As DateTime) As Boolean 2つのDateTimeインスタンスの日付と時刻の値が同じかどうかを示す値を返します。 |
12 |
Public Overrides Function ToString As String 現在のDateTimeオブジェクトの値を同等の文字列表現に変換します。 |
上記のメソッドのリストは網羅的ではありません。DateTime構造のメソッドとプロパティの完全なリストについては、http://msdn.microsoft.com/en-us/library/system.datetime.aspx [Microsoft documentation]をご覧ください。
DateTimeオブジェクトの作成
あなたは、次のいずれかの方法でDateTimeオブジェクトを作成することができます-
- オーバーロードされたDateTimeコンストラクターからDateTimeコンストラクターを呼び出す。
- プロパティまたはメソッドによって返される日付と時刻の値をDateTimeオブジェクトに割り当てることにより。
- 日付と時刻の値の文字列表現を解析します。
- DateTime構造体の暗黙のデフォルトコンストラクターを呼び出します。
次の例はこれを示しています-
上記のコードをコンパイルして実行すると、次の結果が生成されます-
現在の日付と時刻を取得する
次のプログラムは、VB.Netで現在の日付と時刻を取得する方法を示しています-
現在時刻-
上記のコードをコンパイルして実行すると、次の結果が生成されます-
現在の日付-
上記のコードをコンパイルして実行すると、次の結果が生成されます-
日付のフォーマット
日付リテラルはハッシュ記号(##)で囲み、形式M/d/yyyyで指定する必要があります(例:#12/16/2012#)。 そうしないと、アプリケーションが実行されているロケールに応じてコードが変更される場合があります。
たとえば、2012年2月6日の日付に#2/6/2012#の日付リテラルを指定しました。 mm/dd/yyyy形式を使用するロケールでは問題ありません。 ただし、dd/mm/yyyy形式を使用するロケールでは、リテラルは2012年6月2日にコンパイルされます。 ロケールが別の形式、たとえばyyyy/mm/ddを使用する場合、リテラルは無効になり、コンパイラエラーが発生します。
日付リテラルをロケールの形式またはカスタム形式に変換するには、Stringクラスの Format 関数を使用して、定義済みまたはユーザー定義の日付形式を指定します。
次の例はこれを示しています。
上記のコードをコンパイルして実行すると、次の結果が生成されます-
定義済みの日付/時刻形式
次の表は、定義済みの日付と時刻の形式名を示しています。 これらは、 Format 関数のスタイル引数として名前で使用できます-
Format | Description |
---|---|
General Date, or G | Displays a date and/or time. For example, 1/12/2012 07:07:30 AM. |
Long Date,Medium Date, or D | Displays a date according to your current culture’s long date format. For example, Sunday, December 16, 2012. |
Short Date, or d | Displays a date using your current culture’s short date format. For example, 12/12/2012. |
Long Time,Medium Time, orT | Displays a time using your current culture’s long time format; typically includes hours, minutes, seconds. For example, 01:07:30 AM. |
Short Time or t | Displays a time using your current culture’s short time format. For example, 11:07 AM. |
f | Displays the long date and short time according to your current culture’s format. For example, Sunday, December 16, 2012 12:15 AM. |
F | Displays the long date and long time according to your current culture’s format. For example, Sunday, December 16, 2012 12:15:31 AM. |
g | Displays the short date and short time according to your current culture’s format. For example, 12/16/2012 12:15 AM. |
M, m | Displays the month and the day of a date. For example, December 16. |
R, r | Formats the date according to the RFC1123Pattern property. |
s | Formats the date and time as a sortable index. For example, 2012-12-16T12:07:31. |
u | Formats the date and time as a GMT sortable index. For example, 2012-12-16 12:15:31Z. |
U | Formats the date and time with the long date and long time as GMT. For example, Sunday, December 16, 2012 6:07:31 PM. |
Y, y | Formats the date as the year and month. For example, December, 2012. |
ユーザー定義形式などの他の形式については、http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.format.aspx [Microsoft Documentation]を参照してください。
DateAndTimeクラスのプロパティとメソッド
次の表に、 DateAndTime クラスで一般的に使用される*プロパティ*の一部を示します-
Sr.No | Property & Description |
---|---|
1 |
Date システムに応じて現在の日付を表す文字列値を取得または設定します。 |
2 |
Now システムに従って、現在の日付と時刻を含む日付値を返します。 |
3 |
TimeOfDay システムに応じて、現在の時刻を含む日付値を取得または設定します。 |
4 |
Timer 真夜中から経過した秒数を表すDouble値を返します。 |
5 |
TimeString システムに従って現在の時刻を表す文字列値を返すか、設定します。 |
6 |
Today 現在の日付を取得します。 |
次の表は、一般的に使用される DateAndTime クラスの*メソッド*の一部を示しています-
Sr.No | Method Name & Description |
---|---|
1 |
Public Shared Function DateAdd (Interval As DateInterval, Number As Double, DateValue As DateTime) As DateTime 指定された時間間隔が追加された日付と時刻の値を含む日付値を返します。 |
2 |
Public Shared Function DateAdd (Interval As String,Number As Double,DateValue As Object ) As DateTime 指定された時間間隔が追加された日付と時刻の値を含む日付値を返します。 |
3 |
Public Shared Function DateDiff (Interval As DateInterval, Date1 As DateTime, Date2 As DateTime, DayOfWeek As FirstDayOfWeek, WeekOfYear As FirstWeekOfYear ) As Long 2つのDate値の間の時間間隔の数を指定するLong値を返します。 |
4 |
Public Shared Function DatePart (Interval As DateInterval, DateValue As DateTime, FirstDayOfWeekValue As FirstDayOfWeek, FirstWeekOfYearValue As FirstWeekOfYear ) As Integer 指定されたDate値の指定されたコンポーネントを含む整数値を返します。 |
5 |
Public Shared Function Day (DateValue As DateTime) As Integer 月の日を表す1〜31の整数値を返します。 |
6 |
Public Shared Function Hour (TimeValue As DateTime) As Integer 時刻を表す0〜23の整数値を返します。 |
7 |
Public Shared Function Minute (TimeValue As DateTime) As Integer 時間の分を表す0〜59の整数値を返します。 |
8 |
Public Shared Function Month (DateValue As DateTime) As Integer 年の月を表す1〜12の整数値を返します。 |
9 |
Public Shared Function MonthName (Month As Integer, Abbreviate As Boolean) As String 指定された月の名前を含む文字列値を返します。 |
10 |
Public Shared Function Second (TimeValue As DateTime) As Integer 分を表す0〜59の整数値を返します。 |
11 |
Public Overridable Function ToString As String 現在のオブジェクトを表す文字列を返します。 |
12 |
Public Shared Function Weekday (DateValue As DateTime, DayOfWeek As FirstDayOfWeek) As Integer 曜日を表す数値を含む整数値を返します。 |
13 |
Public Shared Function WeekdayName (Weekday As Integer, Abbreviate As Boolean, FirstDayOfWeekValue As FirstDayOfWeek) As String 指定した曜日の名前を含む文字列値を返します。 |
14 |
Public Shared Function Year (DateValue As DateTime) As Integer 年を表す1〜9999の整数値を返します。 |
上記のリストは完全なものではありません。 DateAndTimeクラスのプロパティとメソッドの完全なリストについては、http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateandtime.aspx [Microsoft Documentation]を参照してください。
次のプログラムは、これらのいくつかと方法を示しています-
上記のコードをコンパイルして実行すると、次の結果が生成されます-