| Reference | Downloads | Github

Saving the exact time


I’d like my experiment to save the exact time (date, hour, min and sec) of each trial in a separate column in the .csv file.

I can’t find this option in the experiment settings.

Thanks a lot!

HI @Genevieve_Allaire-Du, you can use the time module for Python

import time
dateNow = time.strftime("%Y-%m-%d_%H.%M.%S") # will produce example time '2018-10-31_10.18.20'

So what you could do, add a code component to your trial routine, and add the following the the “End Routine” tab. Note, you should ``import time``` at the beginning of the experiment, in the “Begin Experiment” tab of your code component.

thisExp.addData("dateTime", time.strftime("%Y-%m-%d_%H.%M.%S")) 


Thanks for your help. I added the code component and edited the “Begin Experiment” and “End Routine” tabs, but I still don’t see the time being recorded in the .csv file.

I attached the experiment file.cont4.psyexp (59.2 KB)


@Genevieve_Allaire-Du, how many csv files are output in your data folder? Can you attach them?

One csv file. 999_ContRun1_2018_Oct_31_1433practiceloop.csv (886 Bytes)

@Genevieve_Allaire-Du, there should be at least two csvs, one for each loop. An alternative is to use the following in “End Routine” to save the data:

if practiceloop.finished:
    trialsloop.addData("dateTime", time.strftime("%Y-%m-%d_%H.%M.%S"))
    practiceloop.addData("dateTime", time.strftime("%Y-%m-%d_%H.%M.%S"))

This will save the output to your specific loop csv files.

Hi David,

Sorry about the confusion. I usually do get two csv files, but I wanted to run a quick check up so I just did the practice loop (the whole experiment takes about 10-15 minutes) so it’s too long to run the whole thing every time I change a parameter. Do you have any clue why I don’t get a column with the time in the practice loop csv file?

Thanks a lot,

Hi David,

I tried to read your message again and follow your instructions as carefully as I could for the first loop of my experiment. I get an error message (see attached).

Could you help me figure out what I do wrong when pasting the lines of code?

Thanks a lot,

area.psyexp (58.2 KB)

Hi @Genevieve_Allaire-Du, have you used the import time in your code in the “Begin Experiment” tab?

Dear David,

Thank you for your patience and graciousness. I did use the import time in my code component in the “Begin Experiment” tab. However, I still get only these columns in my data file:

Cond CorrAnsScanner TrialName Event CorrAns n key_resp_trials.corr_mean key_resp_trials.corr_raw key_resp_trials.corr_std key_resp_trials.keys_raw key_resp_trials.rt_mean key_resp_trials.rt_raw key_resp_trials.rt_std order
ineq 3 Slide108.bmp pract01 j 1 1 1 0 ‘j’ 1.186259031 1.186259031 0 0
inin 1 Slide063.bmp pract02 f 1 0 0 0 ‘j’ 1.352826118 1.352826118 0 1
eqA 2 Slide138.bmp pract03 space 1 0 0 0 ‘j’ 1.186955929 1.186955929 0 2
con 1 Slide007.bmp pract04 f 1 0 0 0 ‘j’ 0.536182165 0.536182165 0 3
eqA 2 Slide159.bmp pract05 space 1 0 0 0 ‘j’ 0.285742998 0.285742998 0 4
inin 3 Slide042.bmp pract06 j 1 1 1 0 ‘j’ 0.236521959 0.236521959 0 5
con 3 Slide004.bmp pract07 j 1 1 1 0 ‘j’ 0.335352182 0.335352182 0 6
ineq 1 Slide105.bmp pract08 f 1 0 0 0 ‘j’ 0.352087021 0.352087021 0 7

area.psyexp (65.6 KB)

If I may recall about the instructions you’ve given

I have put…

these code
import time
into the begin experiment tab

these code
thisExp.addData("realTimeQuestionStarted", time.strftime("%Y-%m-%d_%H.%M.%S"))
into the begin routine tab

unfortunately, I am still not getting any column of ‘realTimeQuestionStarted’ in my output data. Do you mind to help us with this issue?

The issue above is that the other poster was looking at the Excel data file output, which really shouldn’t be used (it attempts some processing of the data, collapsing across trials to calculate means and standard deviations and so on). You should switch off that data output file and use the .csv (trial-by-trial) data file format, which should show your manually recorded values stored for each individual trial.

thanks for the help.
I have used this code. it works in terms of saving but I receive the following alert:
Alert 4210:JavaScript Syntax Error in ‘Begin JS Experiment’ tab. See ‘Line 1: Unexpected token’ in the ‘Begin JS Experiment’ tab.
For further info see 4210: Probable syntax error detected in your JavaScript code — PsychoPy v2021.1
I am using psychopy v2021.1.1.4.
How can I solve it?
Thank you.

Hi @martinadagostini , this Python code is not compatible with JS so will not be translated correctly, but you can do an equivalent in pure JavaScript:

d = new Date();

datestring = d.getDate()  + "-" + (d.getMonth()+1) + "-" + d.getFullYear() + " " +
d.getHours() + ":" + d.getMinutes() + "." + d.getSeconds();

psychoJS.experiment.addData("datestring", datestring);

Hi, thank you for your messsage.
I tried your option. I did this:

  1. inserted the following under begging of experiment
    d = new Date();

datestring = d.getDate() + “-” + (d.getMonth()+1) + “-” + d.getFullYear() + " " +
d.getHours() + “:” + d.getMinutes() + “.” + d.getSeconds();

  1. insertedthe following under end of routine:
    psychoJS.experiment.addData(“datestring”, datestring);

I could only selected py code type which seems weird to me as this should be java.

I received this error:
Alert 4205:Python Syntax Error in ‘Begin Experiment’ tab. See 'd = new Date();
’ on line number 1 of the ‘Begin Experiment’ tab.
For further info see 4205: Probable syntax error detected in your Python code — PsychoPy v2021.1

What am I doing wrong?
Thank you

Hi @martinadagostini, you need to use this code in a JavaScript code component. So you could open the code component as select “both” (right side is JS) or just select JS for JavaScript. But are you saying your code component is not letting you select this?