Crib Sheets
Home Page
Calculator
Chemistry
Convert Units
Electronic
First Aid
Geography
Geology
Mathematics
Music
Nautical
Numbers
Physics
VB.net Programming
Dates Date Example Maths Text Numeric Textbox

Weather
Web Programming
Wiring
DigitalDan Websites
DigitalDan Sites
Hit Counter 211
 
©2000-2013 DigitalDan.co.uk
 
Contact Details
Email address scroller
DigitalDan.uk
VB.NET Handling Dates (An example)

 
The function outlined below is written in VB.net but it should be possible to modify them for other .net languages.
This may need minor adjustments if you use special compiler options (Option Strict) because I have tried to simplify the code for ease of understanding

Using Dates in VB.Net
This function introduces some popular VB.net date handling features.
Create a new form and add textboxes, buttons and labels to match this picture
Picture of project
In the next picture, all the controls have been numbered. Rename every numbered control in accordance with this list...
Picture of project
0frmTest
1btnWWadd
2btnDDadd
3btnMMMadd
4btnYYYYadd
5btnHHadd
6btnMMadd
7btnSSadd
8txtWW
9txtDD
10txtMMM
11txtYYYY
12txtHH
13txtMM
14txtSS
15btnWWsub
16btnDDsub
17btnMMMsub
18btnYYYYsub
19btnHHsub
20btnMMsub
21btnSSsub
22lblDateTime


Now type (or copy/paste) the following code
Public Class frmTest
    Private dat As Date = Now
    Private datMax As Date = dat.AddYears(10)
    Private datMin As Date = dat.AddYears(-10)
    Private Sub frmTest_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtWW.Enabled = False
        txtDD.Enabled = False
        txtMMM.Enabled = False
        txtYYYY.Enabled = False
        txtHH.Enabled = False
        txtMM.Enabled = False
        txtSS.Enabled = False
        showdate()
    End Sub
    Private Sub showdate()
        txtWW.Text = dat.ToString("ddd")
        txtDD.Text = dat.ToString("dd")
        txtMMM.Text = dat.ToString("MMM")
        txtYYYY.Text = dat.ToString("yyyy")
        txtHH.Text = dat.ToString("HH")
        txtMM.Text = dat.ToString("mm")
        txtSS.Text = dat.ToString("ss")
        lblDateTime.Text = dat.ToString("dddd d MMMM yyyy - hh:mm:ss tt")
    End Sub
    Private Sub btnWWadd_Click(sender As Object, e As EventArgs) Handles btnWWadd.Click
        dat = dat.AddDays(7)
        showdate()
    End Sub
    Private Sub btnDDadd_Click(sender As Object, e As EventArgs) Handles btnDDadd.Click
        dat = dat.AddDays(1)
        showdate()
    End Sub
    Private Sub btnMMMadd_Click(sender As Object, e As EventArgs) Handles btnMMMadd.Click
        dat = dat.AddMonths(1)
        showdate()
    End Sub
    Private Sub btnYYYYadd_Click(sender As Object, e As EventArgs) Handles btnYYYYadd.Click
        dat = dat.AddYears(1)
        showdate()
    End Sub
    Private Sub btnHHadd_Click(sender As Object, e As EventArgs) Handles btnHHadd.Click
        dat = dat.AddHours(1)
        showdate()
    End Sub
    Private Sub btnMMadd_Click(sender As Object, e As EventArgs) Handles btnMMadd.Click
        dat = dat.AddMinutes(1)
        showdate()
    End Sub
    Private Sub btnSSadd_Click(sender As Object, e As EventArgs) Handles btnSSadd.Click
        dat = dat.AddSeconds(1)
        showdate()
    End Sub
    Private Sub btnWWsub_Click(sender As Object, e As EventArgs) Handles btnWWsub.Click
        dat = dat.AddDays(-7)
        showdate()
    End Sub
    Private Sub btnDDsub_Click(sender As Object, e As EventArgs) Handles btnDDsub.Click
        dat = dat.AddDays(-1)
        showdate()
    End Sub
    Private Sub btnMMMsub_Click(sender As Object, e As EventArgs) Handles btnMMMsub.Click
        dat = dat.AddMonths(-1)
        showdate()
    End Sub
    Private Sub btnYYYYsub_Click(sender As Object, e As EventArgs) Handles btnYYYYsub.Click
        dat = dat.AddYears(-1)
        showdate()
    End Sub
    Private Sub btnHHsub_Click(sender As Object, e As EventArgs) Handles btnHHsub.Click
        dat = dat.AddHours(-1)
        showdate()
    End Sub
    Private Sub btnMMsub_Click(sender As Object, e As EventArgs) Handles btnMMsub.Click
        dat = dat.AddMinutes(-1)
        showdate()
    End Sub
    Private Sub btnSSsub_Click(sender As Object, e As EventArgs) Handles btnSSsub.Click
        dat = dat.AddSeconds(-1)
        showdate()
    End Sub
End Class

Finally, you can run the program. Clicking the button changes the date. This demonstrates some VB.net date features.


When using date.toString, vb.net looks for these "format specifiers" and replaces all occurences of a date-specifier with the appropriate part of the date or time.
These are some of the better known qualifiers. An internet search could reveal a far longer list.
d    day of month    1 to 31
dd   day of month    01 to 31
ddd  day of week     Sun to Sat          (varies by country)
dddd day of week     Sunday to Saturday  (varies by country)
h    hour            1 to 12
hh   hour            01 to 12
H    hour            0 to 23
HH   hour            00 to 23
m    minute          0 to 59
mm   minute          00 to 59
M    month           1 to 12
MM   month           01 to 12
MMM  month           Jan to Dec          (varies by country)
MMMM month           January to December (varies by country)
s    second          0 to 59
ss   second          00 to 59
tt   am/pm           AM or PM
yy   year            00 to 99
yyyy year            0001 to 9999        (be aware of calendar limitations)
DigitalDan.uk is part of the DigitalDan.co.uk group