95

I want to convert a DateTime instance into a TimeSpan instance, is it possible?

I've looked around but I couldn't find what I want, I only find time difference. More specifically, I want to convert a DateTime instance into milliseconds, to then save it into an IsolatedStorage.

Mafii
  • 7,227
  • 1
  • 35
  • 55
Ayoub.A
  • 1,943
  • 3
  • 27
  • 34

5 Answers5

266

You can just use the TimeOfDay property of date time, which is TimeSpan type:

DateTime.TimeOfDay

This property has been around since .NET 1.1

More information: http://msdn.microsoft.com/en-us/library/system.datetime.timeofday(v=vs.110).aspx

ale10ander
  • 942
  • 5
  • 22
  • 42
  • 5
    DateTime.Now.TimeOfDay || DateTime.Today.TimeOfDay – espaciomore Mar 17 '16 at 15:30
  • 4
    To be painfully & overly clear, you'd use `DateTime myDateTime = DateTime.UtcNow;` or whatever to get something into a *specific instance of `DateTime`*, then you can use **`myDateTime.TimeOfDay;`**. Alternately, you can cut out the middleman & say, **`DateTime.UtcNow.TimeOfDay`**, natch. (That is, `DateTime.TimeOfDay`, the current example code, is "invalid" & won't give you a value the same way `DateTime.UtcNow` does, which makes some sense. You need a specific `DateTime` to grab its `TimeOfDay` value.) – ruffin Oct 11 '16 at 17:21
  • 3
    A word of caution. Using this property will give you, and I quote MSDN, a "time interval that represents the fraction of the day that has elapsed since midnight." There may be times when all you care is about the current day. Other times, you may want to get an "absolute" time (for example, if your values span several days or weeks), in which case @MiMo's suggestion to pick a base would be appropriate. – Gustavo Mori Aug 23 '17 at 23:08
  • 1
    DateTime.TimeOfDay.ToString("hh\\:mm") to format nicely. – JsonStatham Dec 10 '18 at 14:30
45
TimeSpan.FromTicks(DateTime.Now.Ticks)
KoalaBear
  • 2,755
  • 2
  • 25
  • 29
28

To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime).

If you simply want to convert a DateTime to a number you can use the Ticks property.

MiMo
  • 11,793
  • 1
  • 33
  • 48
15

Try the following code.

 TimeSpan CurrentTime = DateTime.Now.TimeOfDay;

Get the time of the day and assign it to TimeSpan variable.

Muhammad Awais
  • 4,238
  • 1
  • 42
  • 37
5

In case you are using WPF and Xceed's TimePicker (which seems to be using DateTime?) as a timespan picker -as I do right now- you can get the total milliseconds (or a TimeSpan) out of it like so:

var milliseconds = DateTimeToTimeSpan(timePicker.Value).TotalMilliseconds;

    TimeSpan DateTimeToTimeSpan(DateTime? ts)
    {
        if (!ts.HasValue) return TimeSpan.Zero;
        else return new TimeSpan(0, ts.Value.Hour, ts.Value.Minute, ts.Value.Second, ts.Value.Millisecond);
    }

XAML :

<Xceed:TimePicker x:Name="timePicker" Format="Custom" FormatString="H'h 'm'm 's's'" />

If not, I guess you could just adjust my DateTimeToTimeSpan() so that it also takes 'days' into account or do sth like dateTime.Substract(DateTime.MinValue).TotalMilliseconds.

NoOne
  • 3,851
  • 1
  • 40
  • 47