4
$\begingroup$

I am trying to manipulate a file to change the time into UnixTime and for that I need the date and time but of course it needs to be date first and time second. I've tried multiplying and I've tried adding, i've even changed the order of which i add and multiply but it always puts the time first. What can I do to fix this.

out = Import["*file name is not really that relevant", "Table"];
out2 = Drop[out, 3];
{date, time, timeElapsed, TPM1, TPM25, TPM10, PM1, PM25, PM10, CO2, TempC, Humidity, NO, NO2, O3, CO2raw, workNO2, auxNO2, workO3, auxO3, labNO, labNO2, labO3, labCO2, labTPM25, labPM25} = Transpose[out2];
realDate = StringDrop[#, -1] & /@ date;
realTime = StringDrop[#, -1] & /@ time;
realTime (realDate)

This prints a list with a bunch of values but the elements all look like "12:00:00 2018/28/8" instead of "2018/28/8 12:00:00"

$\endgroup$
5
$\begingroup$
{realdate, realtime} = {"2018/28/8" , "12:00:00" };

Since realdate and realtime are strings, you can use StringJoin or StringRiffle to get a single string:

datetime = StringJoin[realdate, " ", realtime]

"2018/28/8 12:00:00"

datetime = StringRiffle[{realdate, realtime}]

"2018/28/8 12:00:00"

To have datetime work as a proper date string, specify the date format as

dformat = { "Year", "/", "Month", "/", "Day", " ", "Hour", ":", "Minute", ":", "Second"};

dl = DateList[{datetime, dformat}]

{2020, 4, 8, 12, 0, 0.}

ds = DateString[dl, dformat]

"2020/04/08 12:00:00"

What happens if you multiply/add two strings?

"12:00:00"  "2018/28/8"  

enter image description here

"21:00:00"  "2018/28/8"  

enter image description here

just as in

y x

x y

y + x

x + y

because both operators Plus and Times are Orderless and

"Elements of an expression whose head is orderless are sorted into standard order"

$\endgroup$
3
$\begingroup$

Alternatively, you could just use your current list of ("12:00:00 2018/28/8") formatted DateTimes and pass each element in the list to Interpreter which will correctly parse each into a DateObject.

For example:

Interpreter["DateTime"]["12:00:00 2018/28/8"]

returns the correct DateObject

UnixTime can handle DateObjects. Using your example:

UnixTime[Interpreter["DateTime"]["12:00:00 2018/28/8"]]

1535436000

$\endgroup$
2
$\begingroup$

You could add the element order to your input string, e.g.:

string = "12:00:00 2018/28/8";
UnixTime[{
    string,
    {"Hour","Minute","Second","Year","Day","Month"}
}]

1535482800

You can adjust the assumed time zone as specified in the documentation for UnixTime.

$\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.