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)