VB.Net Tutorial/Date Time/Date Functions

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

Assign Now() to a date variable

public class Test
   public Shared Sub Main

        Dim dteExpiration As Date
        dteExpiration = Now()
        Console.WriteLine(dteExpiration)


   End Sub
End class
11/05/2007 12:30:59 PM

Call to WeekdayName combined with call to Weekday

Option Strict On
Imports System.Globalization
Public Module WeekdayNameFunction
   Public Sub Main()
      
      Dim birthday As Date = #07/28/1993#
      Console.WriteLine(WeekdayName(Weekday(birthday), True))
   End Sub
End Module
Wed

CDate: convert string(15 July, o Date

Module Tester
    Public Sub Main()
        Dim sDate As String = "8/1/86"
        Dim dtDate As Date = CDate(sDate)
        sDate = "15 July, 1215"
        dtDate = CDate(sDate)
        Console.WriteLine("Date now holds {0}", dtDate)
    End Sub
End Module
Date now holds 15/07/1215 12:00:00 AM

CDate: convert string( to date

Module Tester
    Public Sub Main()
        Dim sDate As String = "8/1/86"
        Dim dtDate As Date = CDate(sDate)
        Console.WriteLine("Date now holds {0}", dtDate)
    End Sub
End Module
Date now holds 08/01/1986 12:00:00 AM

CDate: convert string(Tuesday, September 6:45pm) to Date

Module Tester
    Public Sub Main()
        Dim sDate As String = "8/1/86"
        Dim dtDate As Date = CDate(sDate)
        sDate = "Tuesday, September 16, 1845 6:45pm"
        dtDate = CDate(sDate)
        Console.WriteLine("Date now holds {0}", dtDate)
    End Sub
End Module
Date now holds 16/09/1845 6:45:00 PM

DateAdd: add two date together

Option Strict On
Public Module DateAddTest
   Public Sub Main()
      Console.WriteLine(DateAdd(DateInterval.Hour, 36, Date.Now))
      Console.WriteLine(DateAdd(DateInterval.Hour, -36, Date.Now))
   End Sub
End Module
13/05/2007 9:17:01 AM
10/05/2007 9:17:01 AM

Determining the weekday of a date

Option Strict On
Imports System.Globalization
Public Module WeekdayNameFunction
   Public Sub Main()
      " 
      Dim historicalDate As New DateTime(1905, 1, 9, New JulianCalendar())
      Dim dayOfWeek As Integer = Weekday(historicalDate, FirstDayOfWeek.System)
      Console.WriteLine(WeekdayName(dayOfWeek, False, FirstDayOfWeek.System))
   End Sub
End Module
Sunday

Different date between two dates

public class Test
   public Shared Sub Main

        Dim dteExpiration As Date
        Dim lngDays As Long
        lngDays = DateDiff(DateInterval.Day, #12/31/2000#, Now())
        Console.WriteLine(lngDays)

   End Sub
End class
2322

TimeOfDay

public class Test
   public Shared Sub Main
        Console.WriteLine(TimeOfDay)
   End Sub
End Class
01/01/0001 12:22:14 PM

Weekday Function

Option Strict On
Public Module WeekdayFunction
   Public Sub Main
      Dim holiday As Date = #01/01/2006#
      If Weekday(holiday, FirstDayOfWeek.Monday) <= 5 Then
         Console.WriteLine("The holiday on {0}, is during the work week.", _
                           FormatDateTime(holiday, DateFormat.LongDate))
      Else
         Console.WriteLine("The holiday on {0}, occurs during the weekend.", _
                           FormatDateTime(holiday, DateFormat.LongDate))
      End If
   End Sub
End Module
The holiday on January 1, 2006, occurs during the weekend.