Display the Date or Time from a DateTime Object

There are several ways on how to retrieve the date or time part from a datetime object, here are the few methods that I discover through research and experimentation:

1. Using Substring

Display the time part using Substring:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing 
//specify the location of the Form classes
open System.Windows.Forms
let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)
//creates a form
let timerform=new Form(Text="Display Time Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
let timelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)

let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)
//create a timer object and set its interval to 1 second
//by default timer are disabled so you'll need to enable it
let timer1=new Timer(Interval=1000,Enabled=true)
timelabel.Font<-ffont
//change it every 1 min second
timer1.Tick.Add(fun time->
//assigns the current date and time to a variable
let datetime=Convert.ToString(System.DateTime.Now)
//retrieves the value of the datetime variable
//starting from the 11th character
let timepart=datetime.Substring(10)
//display the time
timelabel.Text<-timepart)
                
//adds the exit button to our form
timerform.Controls.Add(timelabel)
timerform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->
//stops the time
timer1.Stop()
//close the form
timerform.Close())                   
//show our form
timerform.Show()
//execute our application
Application.Run(timerform)

Display the time part using the substring function:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing 
//specify the location of the Form classes
open System.Windows.Forms
let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)
//creates a form
let dateform=new Form(Text="Display Date Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//use the random function to generate random numbers
let datelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)

let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)

datelabel.Font<-ffont
dateform.Load.Add(fun time->
//assigns the current date and time to the datetime variable
let datetime=Convert.ToString(System.DateTime.Now)
//retrieves the text from the datetime variable staring from the first
//character to the 11th character
let datepart=datetime.Substring(0,10)
//display the current time
datelabel.Text<-datepart)
                
//adds the exit button to our form
dateform.Controls.Add(datelabel)
dateform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->dateform.Close())                   
//execute our application
Application.Run(dateform)


2. Using Remove

Display the time part using Remove:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing 
//specify the location of the Form classes
open System.Windows.Forms
let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)
//creates a form
let timerform=new Form(Text="Display Time Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
let timelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)

let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)
//create a timer object and set its interval to 1 second
//by default timer are disabled so you'll need to enable it
let timer1=new Timer(Interval=1000,Enabled=true)
timelabel.Font<-ffont
//change it every 1 min second
timer1.Tick.Add(fun time->
//assigns the current date and time to the datetime variable
let datetime=Convert.ToString(System.DateTime.Now)
//removes the first 11 characters from the datetime variable
let timepart=datetime.Remove(0,10)
//display the current time
timelabel.Text<-timepart)
                
//adds the exit button to our form
timerform.Controls.Add(timelabel)
timerform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->
//stops the time
timer1.Stop()
//close the form
timerform.Close())                   
//show our form
timerform.Show()
//execute our application
Application.Run(timerform)

Display the date part using the Remove function:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing 
//specify the location of the Form classes
open System.Windows.Forms
let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)
//creates a form
let dateform=new Form(Text="Display Date Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//use the random function to generate random numbers
let datelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)

let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)

datelabel.Font<-ffont
dateform.Load.Add(fun time->
//assigns the current date and time to the datetime variable
let datetime=Convert.ToString(System.DateTime.Now)
//removes the text from the datetime variable staring from the 11 character
let datepart=datetime.Remove(10)
//display the current time
datelabel.Text<-datepart)
                
//adds the exit button to our form
dateform.Controls.Add(datelabel)
dateform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->dateform.Close())                   
//execute our application
Application.Run(dateform)

SystemSounds in F#

To play a system sound in Visual F#, use the SystemSounds object. For a simple example on using the SystemSounds object, try the following:


// Learn more about F# at http://fsharp.net
//use the f# standard library
open System
//use media classes
open System.Media
//specify the memory location of the classes used in drawing objects
//required to draw the listbox item text
open System.Drawing
//specify the location of the form class
open System.Windows.Forms
//creates a form and assign a "Play System Sounds" caption to it
let soundform=new Form(Text="Play System Sounds",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a label and set its Text to “Count”
let lbl=new Label(Text="System sounds:", Location=new System.Drawing.Point(20,10),AutoSize=true)
//makes a listbox
let soundlistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)
//adds an item to the listbox when the form is loaded
soundform.Load.Add(fun items->
     //adds the items and ignore the passed index position values
                    soundlistbox.Items.Add("Asterisk")|>ignore
                    soundlistbox.Items.Add("Beep")|>ignore
                    soundlistbox.Items.Add("Exclaimation")|>ignore
                    soundlistbox.Items.Add("Hand")|>ignore
                    soundlistbox.Items.Add("Question")|>ignore)
soundlistbox.Click.Add(fun playsound->
                    if soundlistbox.SelectedIndex=0 then
                        SystemSounds.Asterisk.Play()
                        MessageBox.Show("Asterisk", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)|>ignore
                    if soundlistbox.SelectedIndex=1 then
                        SystemSounds.Beep.Play()
                        MessageBox.Show("Beep", "System Sounds", MessageBoxButtons.OK)|>ignore
                    if soundlistbox.SelectedIndex=2 then
                        SystemSounds.Exclamation.Play()
                        MessageBox.Show("Exclaimation", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)|>ignore
                    if soundlistbox.SelectedIndex=3 then
                        SystemSounds.Hand.Play()
                        MessageBox.Show("Hand", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Hand)|>ignore
                    if soundlistbox.SelectedIndex=4 then
                       SystemSounds.Question.Play()
                        MessageBox.Show("Question", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Question)|>ignore) 
                                           
//displays the label to our form
soundform.Controls.Add(lbl)      
//adds the listbox to our form    
soundform.Controls.Add(soundlistbox)           
soundform.Show()
Application.Run(soundform)

This will generate the following output: