1
1
using System ;
2
+ using System . Globalization ;
2
3
3
4
namespace CommonsLibrary
4
5
{
@@ -79,5 +80,39 @@ public static string[] InputDelimitedArray(string msg, char delimiter)
79
80
string [ ] array = input . Split ( delimiter ) ;
80
81
return array ;
81
82
}
83
+
84
+ /// <summary>Gets formatted DateTime from the Console</summary>
85
+ /// <param name="msg">The message displayed to the user</param>
86
+ /// <param name="pattern">The DateTime pattern shown to the user</param>
87
+ /// <param name="culture">The culture-specific information for the DateTime</param>
88
+ /// <returns>A DateTime object representing the string inputted DateTime</returns>
89
+ public static DateTime GetDateTime ( string msg , string pattern , CultureInfo culture )
90
+ {
91
+ bool valid ;
92
+ DateTime result ;
93
+
94
+ do
95
+ {
96
+ string input = Input ( $ "{ msg } ({ pattern } )") ;
97
+ valid = DateTime . TryParseExact ( input , pattern , culture , DateTimeStyles . AllowWhiteSpaces , out result ) ;
98
+
99
+ if ( ! valid )
100
+ Console . WriteLine ( "Invalid date/time format." ) ;
101
+
102
+ } while ( ! valid ) ;
103
+
104
+ return result ;
105
+ }
106
+
107
+ /// <summary>Gets user inputted DateTime and converts it to a unix timestamp</summary>
108
+ /// <param name="msg">The message displayed to the user</param>
109
+ /// <param name="pattern">The DateTime pattern shown to the user</param>
110
+ /// <param name="culture">The culture-specific information for the DateTime</param>
111
+ /// <returns>A unix timestamp representing the inputted DateTime</returns>
112
+ public static long GetUnixDateTime ( string msg , string pattern , CultureInfo culture )
113
+ {
114
+ DateTime dt = GetDateTime ( msg , pattern , culture ) ;
115
+ return ( ( DateTimeOffset ) dt ) . ToUnixTimeSeconds ( ) ;
116
+ }
82
117
}
83
118
}
0 commit comments