# Changing list of different time units to same

My function currently outputs differencies between dates and gives a list as an output. However in the output some of the times are in hours and some in days but this causes problems to further computations. How can I change all the elements in the list to be given in hours? Example output:

{Quantity[1051.25, "Hours"], Quantity[512.77, "Hours"],
Quantity[184.336, "Hours"], Quantity[501.193, "Hours"],
Quantity[675.129, "Hours"], Quantity[585.132, "Hours"],
Quantity[1315.69, "Hours"], Quantity[871.697, "Hours"],
Quantity[1364.36, "Hours"], Quantity[225.999, "Hours"],
Quantity[85.2831, "Days"], Quantity[1328.73, "Hours"],
Quantity[679.439, "Hours"], Quantity[990.738, "Hours"]}


And most of the time there are even more days units than one. I was able to change them by hand using

UnitConvert[Quantity[35.6575, "Days"], "Hours"]


for example but I wasn't able to iterate this trough the list. Also it's not important in which units (or if you are able to output it in integers) the output has if all are the same.

• I'm not sure what have your tried: UnitConvert[list, "Hours"], not to mention UnitConvert[#, "Hours"]&/@list. – Kuba Mar 12 '18 at 7:05

Dates can be represented in many different ways. Two of them that spring to mind, are DateList and DateObject (a third one is DateString but it sometimes can be tricky eg with ambiguous dates, so it won't be discussed here). The former is older than the later, but they both work with DateDifference (or DatePlus, for that matter).

Let diffs contain the differences output (see question):

diffs = {Quantity[1051.25, "Hours"], Quantity[512.77, "Hours"], Quantity[184.336, "Hours"],
Quantity[501.193, "Hours"], Quantity[675.129, "Hours"], Quantity[585.132, "Hours"],
Quantity[1315.69, "Hours"], Quantity[871.697, "Hours"], Quantity[1364.36, "Hours"],
Quantity[225.999, "Hours"], Quantity[85.2831, "Days"], Quantity[1328.73, "Hours"],
Quantity[679.439, "Hours"], Quantity[990.738, "Hours"]};


Then,

input=FoldList[DatePlus[#1, #2] &, DateList[], diffs];


produces a list of dates that have successive differences equal to the ones in diff. Alternatively, one could have used DateObject, instead of DateList.

The following line produces the same output as the one stored in diff, but presented in the same unit, namely "Hours".

DateDifference[#1, #2, "Hour"] & @@@ Partition[input, 2, 1, {1, -1}, {}]

{Quantity[1051.25, "Hours"], Quantity[512.77, "Hours"], Quantity[184.336, "Hours"],
Quantity[501.193, "Hours"], Quantity[675.129, "Hours"], Quantity[585.132, "Hours"],
Quantity[1315.69, "Hours"], Quantity[871.697, "Hours"], Quantity[1364.36, "Hours"],
Quantity[225.999, "Hours"], Quantity[2046.79, "Hours"], Quantity[1328.73, "Hours"],
Quantity[679.439, "Hours"], Quantity[990.738, "Hours"]}


The point of his exercise is to show that depending on how your input data looks like, you can produce the required output (in the desired units) without having the need to transform the output, after the fact.

Assuming that the hypothetical data-that are spaced as shown in diffs-are a list of lists (as in data-see below) then f@@@Partition[data, 2, 1, {1, -1}, {}] can operate on successive pairs of input eg

 BlockRandom[
(* create a list of date-value pairs *)
data = FoldList[{DatePlus[#1[[1]], #2], RandomReal[]} &, {DateObject[], RandomReal[]}, diffs];

(* produce the a list of hour difference-value difference pair for the entries of data *)
{DateDifference[#1[[1]], #2[[1]], "Hour"], -#1[[-1]] + #2[[-1]]} & @@@Partition[data, 2, 1, {1, -1}, {}], RandomSeeding -> 12345667]

{{Quantity[1051.25, "Hours"], -0.280484}, {Quantity[512.77, "Hours"], 0.680484},
{Quantity[184.336, "Hours"], -0.111469}, {Quantity[501.193, "Hours"], -0.587604},
{Quantity[675.129, "Hours"], 0.932254}, {Quantity[585.132, "Hours"], -0.38131},
{Quantity[1315.69, "Hours"], 0.0046715}, {Quantity[871.697, "Hours"], -0.00360121},
{Quantity[1364.36, "Hours"], -0.379791}, {Quantity[225.999, "Hours"], -0.164225},
{Quantity[2046.79, "Hours"], 0.344222}, {Quantity[1328.73, "Hours"], 0.432163},
{Quantity[679.439, "Hours"], 0.0307164}, {Quantity[990.738, "Hours"], 0.170255}}


In this case, f was set up as {DateDifference[#1[[1]], #2[[1]], "Hour"], -#1[[-1]] + #2[[-1]]} & in order to take into account the structure of the input (list of pairs date-value).