Search code examples
c#xamlwindows-phone-8

How to convert System.DateTime To System.TimeSpan explicitly


I want to convert a Date Time time to Time Span format time explicitly . As i have to change time from DateTime to Time span in my reminder app


Solution

  • TimeSpan in general denotes the Time difference between two DateTime. Hence you need to establish a baseline date for the difference.

    You can try using

    TimeSpan ts = new TimeSpan(DateTime.Now.Ticks);
    

    but the above will use a baseline time of 12:00:00 midnight, January 1, 0001


    Trying to answer just by guessing your problem

    Let say you have a event in your remain app created for 15th June 2014

    DateTime eventDate = new DateTime(2014, 6, 6);
    
    // now to show the timespan you can use
    TimeSpan ts = eventDate - DateTime.Now.Date;
    
    // time left to event
    Console.WriteLine("{0} days, {1} hours, {2} minutes", ts.TotalDays, ts.Hours, ts.Minutes);