JayBeeOH

Calculate Age Demo (Console App)

Jan 4th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.79 KB | None | 0 0
  1. '-----------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2018. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Calculate Age Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   04 JAN 2018
  16. '
  17. ' Description:    Demonstrate how to determine a person's age from their birthdate,
  18. '
  19. '                 Documentation is at:
  20. '                   App's Visual Basic .NET code is at: https://pastebin.com/UB2aqCW9  
  21.  
  22. Module Module1
  23.  
  24.     Sub Main()
  25.         Console.Write("Enter your birthdate (mm/dd/yyyy): ")
  26.         Try
  27.             Dim DOB As Date = Date.Parse(Console.ReadLine)
  28.             Console.WriteLine($"Your age is: {GetAge(DOB)}")
  29.         Catch ex As FormatException
  30.             Console.WriteLine("Input Error: Birthday was not entered in correct format.")
  31.         End Try
  32.  
  33.         Console.ReadKey()
  34.     End Sub
  35.  
  36.     Private Function GetAge(birthdate As Date) As Integer
  37.         Dim age As Integer = Today.Year - birthdate.Year
  38.         Dim birthdayThisYear As New Date(Today.Year, birthdate.Month, birthdate.Day)
  39.         If birthdayThisYear > Today Then
  40.             age -= 1
  41.         End If
  42.         Return age
  43.     End Function
  44. End Module
Advertisement
Add Comment
Please, Sign In to add comment