Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /* a helper class, Tester, begun in Lesson 5 and now extended in
- Lessons 6 and 7 of intro C# console programming series found at the
- following URL:
- http://williammillerservices.com/windows-c-console-programming/
- contains the following helper methods:
- + IsInteger()
- + IsDate()
- + IsDateAndLiesInThePast()
- + IsDateAndLiesInTheFuture()
- + GetDifferenceInYears()
- + DaysTillNextBirthday()
- GitHub gist -> https://gist.github.com/kyrathasoft/98ab12cc0b18c5639ce444765d09acc1
- Pastebin.com -> https://pastebin.com/e1WzzVQQ
- note: to use, you would reference the file containing this code
- from your primary program file. If you have the following program...
- using System;
- using TestInput;
- namespace MyNS{
- class MyCls{
- public static void Main(){
- string sNum = "4";
- if(Tester.IsInteger(sNum)){
- Console.WriteLine("Yes, we can parse {0} to an int.", sNum);
- }
- }
- }
- }
- ...and if above program named test.cs and below is tester.cs and in
- same directory, then we'd compile like so:
- csc /out:test.exe test.cs tester4.cs
- Running resulting test.exe would produce: Yes, we can parse 4 to an int.
- Or, we could make tester4.cs code into DLL via:
- csc /t:library tester4.cs
- ...and then compile like so:
- csc /r:tester4.dll test.cs
- */
- namespace TestInput
- {
- public class Tester
- {
- public static bool IsInteger(string p)
- {
- bool result = false;
- int i;
- if (int.TryParse(p, out i))
- {
- result = true;
- }
- return result;
- }
- public static bool IsDate(string p)
- {
- bool result = false;
- DateTime dt;
- if (DateTime.TryParse(p, out dt))
- {
- result = true;
- }
- return result;
- }
- public static bool IsDateAndLiesInThePast(string sDT)
- {
- bool result = false;
- if (IsDate(sDT))
- {
- DateTime dtParsed = DateTime.Parse(sDT);
- if (dtParsed < DateTime.Now)
- {
- result = true;
- }
- }
- return result;
- }
- public static bool IsDateAndLiesInTheFuture(string sDT)
- {
- bool result = false;
- if (IsDate(sDT))
- {
- DateTime dtParsed = DateTime.Parse(sDT);
- if (dtParsed > DateTime.Now)
- {
- result = true;
- }
- }
- return result;
- }
- public static int GetDifferenceInYears(DateTime startDate, DateTime endDate)
- {
- //Excel documentation says "COMPLETE calendar years in between dates"
- int years = endDate.Year - startDate.Year;
- if (startDate.Month == endDate.Month &&// if the start month and the end month are the same
- endDate.Day < startDate.Day// AND the end day is less than the start day
- || endDate.Month < startDate.Month)// OR if the end month is less than the start month
- {
- years--;
- }
- return years;
- }
- public static int DaysTillNextBirthday(DateTime dateOfBirth)
- {
- //full days (whole, 24-hr periods) until next birthday...
- int result = 0;
- DateTime dtToday = DateTime.Now;
- DateTime dtNextBirthday = new DateTime(dtToday.Year, dateOfBirth.Month, dateOfBirth.Day);
- TimeSpan tsDifference = dtNextBirthday - dtToday;
- if (tsDifference.Days < 0)
- {
- //already had birthday this year
- tsDifference = dtNextBirthday.AddYears(1) - dtToday;
- }
- result = tsDifference.Days;
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement