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.

2nd Sep 2016, 9:26 AM
Jansen Tanu
Jansen Tanu - avatar
4 Réponses
+ 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.
2nd Sep 2016, 11:57 AM
LaserHydra
LaserHydra - avatar
+ 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
2nd Sep 2016, 1:46 PM
Liam
Liam - avatar
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.
2nd Sep 2016, 9:54 AM
LaserHydra
LaserHydra - avatar
0
@LaserHydra Thanks. But can you elaborate more?
2nd Sep 2016, 10:54 AM
Jansen Tanu
Jansen Tanu - avatar