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:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use this to enable the intellisense. Very helpful in coding your application  
  5. open System.Drawing   
  6. //specify the location of the Form classes  
  7. open System.Windows.Forms  
  8. let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)  
  9. //creates a form  
  10. let timerform=new Form(Text="Display Time Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let timelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)  
  12.   
  13. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)  
  14. //create a timer object and set its interval to 1 second  
  15. //by default timer are disabled so you'll need to enable it  
  16. let timer1=new Timer(Interval=1000,Enabled=true)  
  17. timelabel.Font<-ffont  
  18. //change it every 1 min second  
  19. timer1.Tick.Add(fun time->  
  20. //assigns the current date and time to a variable  
  21. let datetime=Convert.ToString(System.DateTime.Now)  
  22. //retrieves the value of the datetime variable  
  23. //starting from the 11th character  
  24. let timepart=datetime.Substring(10)  
  25. //display the time  
  26. timelabel.Text<-timepart)  
  27.                   
  28. //adds the exit button to our form  
  29. timerform.Controls.Add(timelabel)  
  30. timerform.Controls.Add(exitbutton)  
  31. //when the exit button is clicked  
  32. exitbutton.Click.Add(fun quit->  
  33. //stops the time  
  34. timer1.Stop()  
  35. //close the form  
  36. timerform.Close())                     
  37. //show our form  
  38. timerform.Show()  
  39. //execute our application  
  40. Application.Run(timerform)  

Display the time part using the substring function:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use this to enable the intellisense. Very helpful in coding your application  
  5. open System.Drawing   
  6. //specify the location of the Form classes  
  7. open System.Windows.Forms  
  8. let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)  
  9. //creates a form  
  10. let dateform=new Form(Text="Display Date Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //use the random function to generate random numbers  
  12. let datelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)  
  13.   
  14. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)  
  15.   
  16. datelabel.Font<-ffont  
  17. dateform.Load.Add(fun time->  
  18. //assigns the current date and time to the datetime variable  
  19. let datetime=Convert.ToString(System.DateTime.Now)  
  20. //retrieves the text from the datetime variable staring from the first  
  21. //character to the 11th character  
  22. let datepart=datetime.Substring(0,10)  
  23. //display the current time  
  24. datelabel.Text<-datepart)  
  25.                   
  26. //adds the exit button to our form  
  27. dateform.Controls.Add(datelabel)  
  28. dateform.Controls.Add(exitbutton)  
  29. //when the exit button is clicked  
  30. exitbutton.Click.Add(fun quit->dateform.Close())                     
  31. //execute our application  
  32. Application.Run(dateform)  


2. Using Remove

Display the time part using Remove:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use this to enable the intellisense. Very helpful in coding your application  
  5. open System.Drawing   
  6. //specify the location of the Form classes  
  7. open System.Windows.Forms  
  8. let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)  
  9. //creates a form  
  10. let timerform=new Form(Text="Display Time Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let timelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)  
  12.   
  13. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)  
  14. //create a timer object and set its interval to 1 second  
  15. //by default timer are disabled so you'll need to enable it  
  16. let timer1=new Timer(Interval=1000,Enabled=true)  
  17. timelabel.Font<-ffont  
  18. //change it every 1 min second  
  19. timer1.Tick.Add(fun time->  
  20. //assigns the current date and time to the datetime variable  
  21. let datetime=Convert.ToString(System.DateTime.Now)  
  22. //removes the first 11 characters from the datetime variable  
  23. let timepart=datetime.Remove(0,10)  
  24. //display the current time  
  25. timelabel.Text<-timepart)  
  26.                   
  27. //adds the exit button to our form  
  28. timerform.Controls.Add(timelabel)  
  29. timerform.Controls.Add(exitbutton)  
  30. //when the exit button is clicked  
  31. exitbutton.Click.Add(fun quit->  
  32. //stops the time  
  33. timer1.Stop()  
  34. //close the form  
  35. timerform.Close())                     
  36. //show our form  
  37. timerform.Show()  
  38. //execute our application  
  39. Application.Run(timerform)  

Display the date part using the Remove function:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use this to enable the intellisense. Very helpful in coding your application  
  5. open System.Drawing   
  6. //specify the location of the Form classes  
  7. open System.Windows.Forms  
  8. let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)  
  9. //creates a form  
  10. let dateform=new Form(Text="Display Date Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //use the random function to generate random numbers  
  12. let datelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)  
  13.   
  14. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)  
  15.   
  16. datelabel.Font<-ffont  
  17. dateform.Load.Add(fun time->  
  18. //assigns the current date and time to the datetime variable  
  19. let datetime=Convert.ToString(System.DateTime.Now)  
  20. //removes the text from the datetime variable staring from the 11 character  
  21. let datepart=datetime.Remove(10)  
  22. //display the current time  
  23. datelabel.Text<-datepart)  
  24.                   
  25. //adds the exit button to our form  
  26. dateform.Controls.Add(datelabel)  
  27. dateform.Controls.Add(exitbutton)  
  28. //when the exit button is clicked  
  29. exitbutton.Click.Add(fun quit->dateform.Close())                     
  30. //execute our application  
  31. 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:


  1. // Learn more about F# at http://fsharp.net  
  2. //use the f# standard library  
  3. open System  
  4. //use media classes  
  5. open System.Media  
  6. //specify the memory location of the classes used in drawing objects  
  7. //required to draw the listbox item text  
  8. open System.Drawing  
  9. //specify the location of the form class  
  10. open System.Windows.Forms  
  11. //creates a form and assign a "Play System Sounds" caption to it  
  12. let soundform=new Form(Text="Play System Sounds",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  13. //creates a label and set its Text to “Count”  
  14. let lbl=new Label(Text="System sounds:", Location=new System.Drawing.Point(20,10),AutoSize=true)  
  15. //makes a listbox  
  16. let soundlistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)  
  17. //adds an item to the listbox when the form is loaded  
  18. soundform.Load.Add(fun items->  
  19.      //adds the items and ignore the passed index position values  
  20.                     soundlistbox.Items.Add("Asterisk")|>ignore  
  21.                     soundlistbox.Items.Add("Beep")|>ignore  
  22.                     soundlistbox.Items.Add("Exclaimation")|>ignore  
  23.                     soundlistbox.Items.Add("Hand")|>ignore  
  24.                     soundlistbox.Items.Add("Question")|>ignore)  
  25. soundlistbox.Click.Add(fun playsound->  
  26.                     if soundlistbox.SelectedIndex=0 then  
  27.                         SystemSounds.Asterisk.Play()  
  28.                         MessageBox.Show("Asterisk""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)|>ignore  
  29.                     if soundlistbox.SelectedIndex=1 then  
  30.                         SystemSounds.Beep.Play()  
  31.                         MessageBox.Show("Beep""System Sounds", MessageBoxButtons.OK)|>ignore  
  32.                     if soundlistbox.SelectedIndex=2 then  
  33.                         SystemSounds.Exclamation.Play()  
  34.                         MessageBox.Show("Exclaimation""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)|>ignore  
  35.                     if soundlistbox.SelectedIndex=3 then  
  36.                         SystemSounds.Hand.Play()  
  37.                         MessageBox.Show("Hand""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Hand)|>ignore  
  38.                     if soundlistbox.SelectedIndex=4 then  
  39.                        SystemSounds.Question.Play()  
  40.                         MessageBox.Show("Question""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Question)|>ignore)   
  41.                                              
  42. //displays the label to our form  
  43. soundform.Controls.Add(lbl)        
  44. //adds the listbox to our form      
  45. soundform.Controls.Add(soundlistbox)             
  46. soundform.Show()  
  47. Application.Run(soundform)  

This will generate the following output: