# How to save time variable

**URL:** https://discourse.psychopy.org/t/how-to-save-time-variable/24042
**Category:** Builder
**Tags:** timing
**Created:** [July 14, 2021, 9:00pm UTC](https://discourse.psychopy.org/t/how-to-save-time-variable/24042 "2021-07-14T21:00:57Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![lucia96](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@lucia96](https://discourse.psychopy.org/u/lucia96)
#### Post date: [July 14, 2021, 9:00pm UTC](https://discourse.psychopy.org/t/how-to-save-time-variable/24042/1 "2021-07-14T21:00:57Z")

</div>

OS: Windows 10  
PsychoPy 2020.2.10  
What are you trying to achieve?:  
I want to save the time when something happens (a message is received) in the data Excel file.  
I tried the following code:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/f/9/f92e6a83f4a1e9fed226861f92ad16189a824fa0.png)

But I’m not sure it works because the time saved looks very different compared to the time saved by other “\_.started” variables.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/9/d90fead14a3bd1506ced16e74c12bd942436bbf0.png)

Is there any way how I can save the time in the same “timeline”?

I have also tried:

```auto
thisExp.addData('master.started', master.tStartRefresh)

```

But it seems to interfere with the code because the next step doesn’t happen as it is supposed to.

Maybe I’m going about it the wrong way but I haven’t found a better way to do it, if anyone could help me I would be very grateful!  
Lucia

---

<div class="post-metadata">

### Author: ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)
#### Post date: [July 16, 2021, 10:56am UTC](https://discourse.psychopy.org/t/how-to-save-time-variable/24042/2 "2021-07-16T10:56:24Z")

</div>

Hi There,

There will be some differences between the .started variables and the t variables because they are subtly different - you might find this post helpful with info on various timing variables! [Saving stimuli time from the beginning of the experiment - #8 by Becca](https://discourse.psychopy.org/t/saving-stimuli-time-from-the-beginning-of-the-experiment/20973/8)

Becca

---

<div class="post-metadata">

### Author: ![lucia96](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@lucia96](https://discourse.psychopy.org/u/lucia96)
#### Post date: [July 16, 2021, 2:00pm UTC](https://discourse.psychopy.org/t/how-to-save-time-variable/24042/3 "2021-07-16T14:00:06Z")

</div>

Hi Becca,

Thank you very much for your reply, very helpful!!

Exactly!! Thanks to your explanation I thought that I will save the time at which the code component is started (using “.started”) and then add the time at which (the message is received) it is saved using “t” (which from what I understood is the internal clock of the Routine) or is it not correct? This way I think I can get the time when the “message” is received in reference to the global clock, right?

Lucia

---

<div class="post-metadata">

### Author: ![lucia96](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@lucia96](https://discourse.psychopy.org/u/lucia96)
#### Post date: [July 22, 2021, 9:37am UTC](https://discourse.psychopy.org/t/how-to-save-time-variable/24042/4 "2021-07-22T09:37:11Z")

</div>

Hi Becca,  
I have one routine called “trial1” that only has a code component called “code\_1”.  
I would like to save the time when the “code\_1” component starts or the trial1 routine starts and finishes.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/0/0/003e0c337425b7e8224607e710ff848a63cdc393.png)

I tried this code, but I got an error saying “code\_1”/“trial1” doesn’t exist. Is there anything else I should add?

Best,  
Lucia

---

<div class="post-metadata">

### Author: ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)
#### Post date: [July 22, 2021, 2:13pm UTC](https://discourse.psychopy.org/t/how-to-save-time-variable/24042/5 "2021-07-22T14:13:45Z")

</div>

Hi Lucia,  
Ah! I don’t think code components actually get written as a component to your script - the code within the component is simply written as code itself not as a component - so it doesn’t have a start time and stop time.

Becca

---

<div class="post-metadata">

### Author: ![lucia96](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@lucia96](https://discourse.psychopy.org/u/lucia96)
#### Post date: [July 22, 2021, 2:56pm UTC](https://discourse.psychopy.org/t/how-to-save-time-variable/24042/6 "2021-07-22T14:56:56Z")

</div>

Hi Becca,

I didn’t know that the code is not considered as a component, but makes sense! Thank you very much for this useful information! Can I save the start time and stop time for the whole routine “trial1”?

Best,  
Lucia

---

<div class="post-metadata">

### Author: ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)
#### Post date: [July 22, 2021, 2:59pm UTC](https://discourse.psychopy.org/t/how-to-save-time-variable/24042/7 "2021-07-22T14:59:12Z")

</div>

Yes you should be able to! if in the begin routine you write:  
`startTime = trial1Clock.getTime()` and in the endRoutine write `endTime = trial1Clock.getTime()` then save those values 🙂

Becca
