Little bit late but since you didn't get any attention, so..
Is it possible to exactly clone the DateTime value to Timespan data
type
Well, since your example works exactly since DateTime.MinValue
has 0
as a Ticks
. It is equivalent to;
TimeSpan timeSpan = TimeSpan.FromTicks(((DateTime)startTimeValue)Ticks);
how do it display in DD MM YYYY HH MM SS format
First of all, there is no DD
, YYYY
and SS
as a custom date and time format specifiers. They are represented as dd
, yyyy
and ss
.
TimeSpan
is quite different than DateTime
. It is a time interval but DateTime
is a point in time. As you can see, a time interval can't have any month and year parts. In which Calendar
? Not all calenders have 12 month. In which month exactly? How many months has 29 days are in GregorianCalender
? Or ChineseLunisolarCalendar
? It depends, right? Month and years concepts are ambigious for TimeSpan.
But still, you can format your TimeSpan
as day, hour, minute and seconds with TimeSpan.ToString()
method like;
timeSpan.ToString(@"d\.hh\:mm\:ss")
As a better alternative, you can use Nodatime which has Period
class and PeriodUnits
enum that you can calculate these like;
using System;
using NodaTime;
public class Program
{
public static void Main()
{
LocalDate start = new LocalDate(2010, 11, 20);
LocalDate end = new LocalDate(2015, 7, 13);
Period period = Period.Between(start, end,
PeriodUnits.Months | PeriodUnits.Years);
Console.WriteLine("{0} - {1}", period.Months, period.Years); // 7 - 4
}
}