# Updating Digital Clock Reading to Always Match Current Time

**URL:** <https://discourse.processing.org/t/updating-digital-clock-reading-to-always-match-current-time/12363>\
**Category:** Coding Questions\
**Created:** [June 28, 2019, 3:47pm UTC](https://discourse.processing.org/t/updating-digital-clock-reading-to-always-match-current-time/12363 "2019-06-28T15:47:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![per0\_22](https://avatars.discourse-cdn.com/v4/letter/p/f1d935/32.png) [@per0\_22](https://discourse.processing.org/u/per0_22)\
**Post date:** [June 28, 2019, 3:47pm UTC](https://discourse.processing.org/t/updating-digital-clock-reading-to-always-match-current-time/12363/1 "2019-06-28T15:47:10Z")

</div>

Hello. This is my first post so please correct me if my formatting is awful.

I am trying to have a simple date and time print onto the monitor. I am able to print the values using their corresponding functions from processing, however I would like them to update an match the actual time on each iteration of draw(). Right now they will match the time I initialized the program and remain static.

```auto
int M = month(); //month
int D = day(); //day
int Y = year(); //year
int Hr = hour(); //hour          
int Min = minute(); //minute
int Sec = second(); //second 
PFont dateAndTime;

void setup() {
  size(400, 400);
  background(0);
  dateAndTime = createFont("Monospaced", 20);
}

void draw() {
  background(0);
  displayDate();
  displayTime();
  textAlign(CENTER);
}

//-----------------------------------------------------------
void displayDate() {
  String Ms = str(M);
  String Ds = str(D);
  String Ys = str(Y);
  String MDY = Ms + "/" + Ds+ "/" + Ys;

  textFont(dateAndTime);
  text(MDY, 200, 180);
}

//------------------------------------------------------------
void displayTime() {
  String Hrs = str(Hr);
  String Mins = str(Min);
  if (Mins.length() < 2) {
    Mins = "0" + Mins;
  }
  String Secs = str(Sec);
  if (Secs.length() < 2) {
    Secs = "0" + Secs;
  }
  String HMS = Hrs + ":" + Mins + ":" + Secs;
  textFont(dateAndTime);
  text(HMS, 200, 200);
}

```

Any ideas on how to do this?

---

<div class="post-metadata">

**Author:** ![per0\_22](https://avatars.discourse-cdn.com/v4/letter/p/f1d935/32.png) [@per0\_22](https://discourse.processing.org/u/per0_22)\
**Post date:** [June 28, 2019, 3:56pm UTC](https://discourse.processing.org/t/updating-digital-clock-reading-to-always-match-current-time/12363/2 "2019-06-28T15:56:34Z")

</div>

Well this is embarrassing but at least it is fixed. I had declared the variables that store the values of minutes, seconds, etc. OUTSIDE of draw() so they weren’t constantly updating. I moved them inside now and it works.

I don’t know how to delete a forum post so hopefully this will help someone in the future.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 30, 2019, 5:21am UTC](https://discourse.processing.org/t/updating-digital-clock-reading-to-always-match-current-time/12363/3 "2019-06-30T05:21:02Z")

</div>

Glad you figured it out!

Just to be clear – you can also use those functions (eg `seconds()`) directly in `displayTime()` and `displayDate()` (which are called by draw) without saving them to variables first – no local or global variables needed if all you want is display.

---

<div class="post-metadata">

**Author:** ![Tjelix](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Tjelix](https://discourse.processing.org/u/Tjelix)\
**Post date:** [May 18, 2021, 12:48pm UTC](https://discourse.processing.org/t/updating-digital-clock-reading-to-always-match-current-time/12363/4 "2021-05-18T12:48:38Z")

</div>

Hello!

Can you show what you did in the code to make it work? I am very new to coding XD

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 18, 2021, 9:35pm UTC](https://discourse.processing.org/t/updating-digital-clock-reading-to-always-match-current-time/12363/5 "2021-05-18T21:35:33Z")

</div>

Hello,

Processing has resources (tutorials, references, examples, etc.) here:  
_[processing.org](http://processing.org)_

References:

- [VariableScope \ Examples \ Processing.org](https://processing.org/examples/variablescope.html)
- [setup() \ Language (API) \ Processing 3+](https://processing.org/reference/setup_.html)
- [draw() \ Language (API) \ Processing 3+](https://processing.org/reference/draw_.html)
- Look up the rest…

This should give you some insight after you read the references:

```auto
// Global variable declarations
// Set before setup() and draw()
int sec = second();

// Runs once
void setup() 
  {
  size(200, 200);
  textAlign(CENTER, CENTER);
  textSize(48);
  }

// Loop that repeats 60 frames per second (default)
void draw() 
  {
  background(0);
  //sec = second(); // Try this as part of code! It is a commented now
  text(sec, 100, 100);
  }

```
