0
Datatype for date & time?
Which types should I use to handle date & time? I'll need to deal with lots of calculation, such as 20/06/2016 +1 = 21/06/2016 Any help is greatly appreciated.
4 Answers
+ 2
https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx
Which to use depends most likely on your circumstances.
+ 1
As mentioned, DateTime and TimeSpan. Here are a couple of examples:
using DateTime only:
DateTime date = new DateTime( 2016, 06, 20 ); // parameters: year, month, day
Console.WriteLine( "Original date: " + date.ToShortDateString() ); // The ToShortDateString() method displays only the date
date = date.AddDays( 1 ); // Add 1 day. There are also methods for AddYears, AddMonths, etc
Console.WriteLine( "New date: " + date.ToShortDateString() ); // The ToShortDateString() method displays only the date
using DateTime + TimeSpan:
DateTime date = new DateTime( 2016, 06, 20 ); // parameters: year, month, day
TimeSpan tSpan = new TimeSpan( 1, 0, 0, 0 ); // parameters: days, hours, minutes, seconds
Console.WriteLine( "Original date: " + date.ToShortDateString() ); // The ToShortDateString() method displays only the date
date += tSpan; // add timespan (1 day) to date
Console.WriteLine( "New date: " + date.ToShortDateString() ); // The ToShortDateString() method displays only the date
0
There are two time types. One respresenting a date and the other one a time span.
TimeSpan is the type which represents a time span and DateTime is the type which represents a date.
0
@LaserHydra
Thanks. But can you elaborate more?