Advertisement
kyrathasoft

tester4

Jul 23rd, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. /* a helper class, Tester, begun in Lesson 5 and now extended in
  3. Lessons 6 and 7 of intro C# console programming series found at the
  4. following URL:
  5. http://williammillerservices.com/windows-c-console-programming/
  6.  
  7. contains the following helper methods:
  8. + IsInteger()
  9. + IsDate()
  10. + IsDateAndLiesInThePast()
  11. + IsDateAndLiesInTheFuture()
  12. + GetDifferenceInYears()
  13. + DaysTillNextBirthday()
  14.  
  15. GitHub gist -> https://gist.github.com/kyrathasoft/98ab12cc0b18c5639ce444765d09acc1
  16. Pastebin.com -> https://pastebin.com/e1WzzVQQ
  17.  
  18. note: to use, you would reference the file containing this code
  19. from your primary program file. If you have the following program...
  20.  
  21. using System;
  22. using TestInput;
  23.  
  24. namespace MyNS{
  25.     class MyCls{
  26.         public static void Main(){
  27.             string sNum = "4";
  28.             if(Tester.IsInteger(sNum)){
  29.                 Console.WriteLine("Yes, we can parse {0} to an int.", sNum);
  30.             }
  31.         }
  32.     }
  33. }
  34.  
  35. ...and if above program named test.cs and below is tester.cs and in
  36. same directory, then we'd compile like so:
  37.  
  38. csc /out:test.exe test.cs tester4.cs
  39.  
  40. Running resulting test.exe would produce: Yes, we can parse 4 to an int.
  41.  
  42. Or, we could make tester4.cs code into DLL via:
  43. csc /t:library tester4.cs
  44.  
  45. ...and then compile like so:
  46. csc /r:tester4.dll test.cs
  47.  
  48. */
  49.  
  50.  
  51. namespace TestInput
  52. {
  53.     public class Tester
  54.     {
  55.         public static bool IsInteger(string p)
  56.         {
  57.             bool result = false;
  58.             int i;
  59.  
  60.             if (int.TryParse(p, out i))
  61.             {
  62.                 result = true;
  63.             }
  64.  
  65.             return result;
  66.         }
  67.        
  68.         public static bool IsDate(string p)
  69.         {
  70.             bool result = false;
  71.             DateTime dt;
  72.  
  73.             if (DateTime.TryParse(p, out dt))
  74.             {
  75.                 result = true;
  76.             }
  77.      
  78.             return result;
  79.         }
  80.        
  81.         public static bool IsDateAndLiesInThePast(string sDT)
  82.         {
  83.             bool result = false;
  84.  
  85.             if (IsDate(sDT))
  86.             {
  87.                 DateTime dtParsed = DateTime.Parse(sDT);
  88.  
  89.                 if (dtParsed < DateTime.Now)
  90.                 {
  91.                     result = true;
  92.                 }
  93.             }
  94.  
  95.             return result;
  96.         }
  97.        
  98.         public static bool IsDateAndLiesInTheFuture(string sDT)
  99.         {
  100.             bool result = false;
  101.  
  102.             if (IsDate(sDT))
  103.             {
  104.                 DateTime dtParsed = DateTime.Parse(sDT);
  105.  
  106.                 if (dtParsed > DateTime.Now)
  107.                 {
  108.                     result = true;
  109.                 }
  110.             }
  111.  
  112.             return result;
  113.         }
  114.  
  115.        
  116.         public static int GetDifferenceInYears(DateTime startDate, DateTime endDate)
  117.         {
  118.             //Excel documentation says "COMPLETE calendar years in between dates"
  119.             int years = endDate.Year - startDate.Year;
  120.  
  121.             if (startDate.Month == endDate.Month &&// if the start month and the end month are the same
  122.                 endDate.Day < startDate.Day// AND the end day is less than the start day
  123.                 || endDate.Month < startDate.Month)// OR if the end month is less than the start month
  124.             {
  125.                 years--;
  126.             }
  127.  
  128.             return years;
  129.         }
  130.        
  131.         public static int DaysTillNextBirthday(DateTime dateOfBirth)
  132.         {
  133.             //full days (whole, 24-hr periods) until next birthday...
  134.             int result = 0;
  135.             DateTime dtToday = DateTime.Now;
  136.             DateTime dtNextBirthday = new DateTime(dtToday.Year, dateOfBirth.Month, dateOfBirth.Day);
  137.             TimeSpan tsDifference = dtNextBirthday - dtToday;
  138.  
  139.             if (tsDifference.Days < 0)
  140.             {
  141.                 //already had birthday this year
  142.                 tsDifference = dtNextBirthday.AddYears(1) - dtToday;
  143.             }
  144.             result = tsDifference.Days;
  145.             return result;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement