2
$\begingroup$

I am importing a list of timestamps (hour:minutes:seconds) from a series of text files. I have the list:

{"12:41:43", "15:35:40", "18:28:38", "21:21:40", "0:14:51", 
"3:07:51", "6:00:43", "8:52:17", "11:43:53", "14:35:26", "17:25:18", "20:15:13"}

I need to figure out how to get this format into a single decimal value in minutes. In excel, this can be done simply by changing the "time" format to "number". I've explored a few options related to Date[] and other functions, but nothing seems to do what I need it to do. If there's no easy way, I imagine I could break up the sections, convert each to minutes, and add them, but such a feat is currently beyond my mathematica expertise.

If anyone is wondering, the goal is to have a single mathematica spreadsheet in which we can import the raw data file from a testing apparatus, extract the values (a certain parameter vs time) and then plot them. I'm good on the parameter, I'm just stuck getting time!

$\endgroup$
  • 1
    $\begingroup$ Does this give what you need: times={"12:41:43", "15:35:40", "18:28:38", "21:21:40", "0:14:51", "3:07:51", "6:00:43", "8:52:17", "11:43:53", "14:35:26", "17:25:18", "20:15:13"}; minutes=(AbsoluteTime/@times -AbsoluteTime[{2015,1,1}])/60.? $\endgroup$ – kglr May 12 '15 at 17:25
4
$\begingroup$

You can get the value in number of minutes since midnight. I not certain if this is the same as formatting a time to decimal in Excel.

You have a time format as a string so can StringSplit these into their components and convert them to from strings to integers with FromDigits. This will give a matrix of vectors with {hour, minute, second} entries. These convert to minutes by applying the following factors respectively {60, 1, 1/60}. Using the Dot product we can apply these factors to each row and sum the row. The N function ensures we get numerical values instead of fractions (a by product of the 1/60).

timestamps = {"12:41:43", "15:35:40", "18:28:38", "21:21:40", 
  "0:14:51", "3:07:51", "6:00:43", "8:52:17", "11:43:53", "14:35:26", 
  "17:25:18", "20:15:13"}

(FromDigits /@ StringSplit[#, ":"] & /@ timestamps).{60, 1, 1/60}  // N

(* {761.717, 935.667, 1108.63, 1281.67, 14.85, 187.85, 360.717, 532.283, \
703.883, 875.433, 1045.3, 1215.22} *)

Hope this helps.

$\endgroup$
3
$\begingroup$
{60., 1, 1/60}.DateList[#][[4 ;;]] & /@ {"12:41:43", "15:35:40", 
  "18:28:38", "21:21:40", "0:14:51", "3:07:51", "6:00:43", "8:52:17", 
  "11:43:53", "14:35:26", "17:25:18", "20:15:13"}

yields:

(* {761.717, 935.667, 1108.63, 1281.67, 14.85, 187.85, 360.717, 532.283, \
703.883, 875.433, 1045.3, 1215.22} *)
$\endgroup$
2
$\begingroup$
times={"12:41:43", "15:35:40", "18:28:38", "21:21:40", "0:14:51", "3:07:51", 
       "6:00:43", "8:52:17", "11:43:53", "14:35:26", "17:25:18", "20:15:13"}; 

time0 = "00:00:00";
minutes = QuantityMagnitude @ DateDifference[time0, #, "Minute"]& /@ times

{761.717, 935.667, 1108.63, 1281.67, 14.85, 187.85, 360.717, 532.283, 703.883, 875.433, 1045.3, 1215.22}

Alternatively, you can use AbsoluteTime to get the same result:

minutes = (AbsoluteTime /@ times - AbsoluteTime @ time0) / 60.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.