VB.Net/Development/Date Time

Материал из VB Эксперт
Версия от 15:45, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Assign a value according to the current day of the week.

<source lang="vbnet">

Module Module1

   Sub Main()
       If (Weekday(Now) = vbSaturday) Or (Weekday(Now) = vbSunday) Then
           System.Console.WriteLine("weekend")
       Else
           System.Console.WriteLine("working day")
       End If
   End Sub

End Module


 </source>


Date Time Detail: Long Date String, day of week, days in month

<source lang="vbnet"> Imports System.IO Module Module1

   Sub Main()
       Dim DateInfo As New DateTime(Now.Ticks)
       Console.WriteLine("Date: " & DateInfo.Month & "/" & _
          DateInfo.Day & "/" & DateInfo.Year)
       Console.WriteLine("Date: " & DateInfo.ToLongDateString())
       Console.WriteLine("Today is: " & DateInfo.DayOfWeek)
       Console.WriteLine("Days in April, 2002: " & DateInfo.DaysInMonth(2002, 4))
   End Sub

End Module


 </source>


Date time: Now, Today and UTC Now

<source lang="vbnet">

Public Class MainClass

  Public Shared Sub Main()
       "Date Time examples
       Dim dteNow As Date = Now()
       Dim dteToday As Date = Today()
       Dim dteGMT As DateTime = DateTime.UtcNow()
       Console.WriteLine(dteNow)
       Console.WriteLine(dteToday)
       Console.WriteLine(dteGMT)
       
  End Sub

End Class


 </source>


Display the various properties for a Date

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main()
       Dim dteData As Date
       "Get the current date and time
       dteData = Now
       "Display the various properties
       System.Console.WriteLine("Month: " & dteData.Month)
       System.Console.WriteLine("Day: " & dteData.Day)
       System.Console.WriteLine("Year: " & dteData.Year)
       System.Console.WriteLine("Hour: " & dteData.Hour)
       System.Console.WriteLine("Minute: " & dteData.Minute)
       System.Console.WriteLine("Second: " & dteData.Second)
       System.Console.WriteLine("Day of week: " & dteData.DayOfWeek)
       System.Console.WriteLine("Day of year: " & dteData.DayOfYear)
   End Sub

End Class


 </source>


Finding the Last Day of the Month

<source lang="vbnet"> Module MainModule

   Public Function LastDayOfMonth(ByVal MonthYear As String) As Integer
       Return DatePart("d ", DateAdd("d ", -1, DateAdd("m ", 1, DateAdd("d ", -DatePart("d ", MonthYear) + 1, MonthYear))))
   End Function

End Module


 </source>


Get Date Time Now

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
      Dim currentTime as System.DateTime = System.DateTime.Now
      Console.WriteLine("currentTime Year :" & currentTime.Year)
      Console.WriteLine("currentTime Month:" &  currentTime.Month)
      Console.WriteLine("currentTime Date:" & currentTime.Date)
      Console.WriteLine("currentTime Hour:" & currentTime.Hour)
      Console.WriteLine("currentTime Minute:" & currentTime.Minute)
      Console.WriteLine("currentTime Second:" & currentTime.Second)
   End Sub

End Class


 </source>


Is Leap Year

<source lang="vbnet"> Imports System.IO Module Module1

   Sub Main()
       Dim Dt As DateTime = Now()
       If (Dt.IsLeapYear(Dt.Year)) Then
           Console.WriteLine("This is a leap year")
       Else
           Console.WriteLine("This is not a leap year")
       End If
   End Sub

End Module


 </source>