Tutorial by Examples

// Calculate what day of the week is 36 days from this instant. System.DateTime today = System.DateTime.Now; System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0); System.DateTime answer = today.Add(duration); System.Console.WriteLine("{0:dddd}", answer);
Add days into a dateTime object. DateTime today = DateTime.Now; DateTime answer = today.AddDays(36); Console.WriteLine("Today: {0:dddd}", today); Console.WriteLine("36 days from today: {0:dddd}", answer); You also can subtract days passing a negative value: DateTime today...
double[] hours = {.08333, .16667, .25, .33333, .5, .66667, 1, 2, 29, 30, 31, 90, 365}; DateTime dateValue = new DateTime(2009, 3, 1, 12, 0, 0); foreach (double hour in hours) Console.WriteLine("{0} + {1} hour(s) = {2}", dateValue, hour, ...
string dateFormat = "MM/dd/yyyy hh:mm:ss.fffffff"; DateTime date1 = new DateTime(2010, 9, 8, 16, 0, 0); Console.WriteLine("Original date: {0} ({1:N0} ticks)\n", date1.ToString(dateFormat), date1.Ticks); DateTime date2 = date1.AddMilliseconds(1); Console....
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0); DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0); int result = DateTime.Compare(date1, date2); string relationship; if (result < 0) relationship = "is earlier than"; else if (result == 0) relationship = "is the...
const int July = 7; const int Feb = 2; int daysInJuly = System.DateTime.DaysInMonth(2001, July); Console.WriteLine(daysInJuly); // daysInFeb gets 28 because the year 1998 was not a leap year. int daysInFeb = System.DateTime.DaysInMonth(1998, Feb); Console.WriteLine(daysInFeb); // daysIn...
Add years on the dateTime object: DateTime baseDate = new DateTime(2000, 2, 29); Console.WriteLine("Base Date: {0:d}\n", baseDate); // Show dates of previous fifteen years. for (int ctr = -1; ctr >= -15; ctr--) Console.WriteLine("{0,2} year(s) ago:{1:d}", ...
Wikipedia currently defines a pure function as follows: The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions...
// Converts the string representation of a date and time to its DateTime equivalent var dateTime = DateTime.Parse("14:23 22 Jul 2016"); Console.WriteLine(dateTime.ToString());
// Converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded string[] dateTimeStrings = new []{ "14:23 22 Jul 2016", "99:23 2x Jul 2016", "22/7/2016 14:23:0...
You might want to use it when parsing DateTimes from different cultures (languages), following example parses Dutch date. DateTime dateResult; var dutchDateString = "31 oktober 1999 04:20"; var dutchCulture = CultureInfo.CreateSpecificCulture("nl-NL"); DateTime.TryParse(dutch...
// This iterates through a range between two DateTimes // with the given iterator (any of the Add methods) DateTime start = new DateTime(2016, 01, 01); DateTime until = new DateTime(2016, 02, 01); // NOTICE: As the add methods return a new DateTime you have // to overwrite dt in the itera...
using System; public class Program { public static void Main() { var date = new DateTime(2016,12,31); Console.WriteLine(date.ToString()); //Outputs: 12/31/2016 12:00:00 AM Console.WriteLine(date.ToShortDateString()); //Out...
To get the current date you use the DateTime.Today property. This returns a DateTime object with today's date. When this is then converted .ToString() it is done so in your system's locality by default. For example: Console.WriteLine(DateTime.Today); Writes today's date, in your local format to...
Standard DateTime Formatting DateTimeForma­tInfo specifies a set of specifiers for simple date and time formating. Every specifier correspond to a particular DateTimeFormatInfo format pattern. //Create datetime DateTime dt = new DateTime(2016,08,01,18,50,23,230); var t = String.Format("{0...
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly. Convert a specific format string to equivalent DateTime Le...
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether th...

Page 1 of 1