0
let days
let months
let years

function setup() {
// Our anniversary is 4/7/2019
  createCanvas(600, 600);
  days = day()-7
  months = month()-4
  years = year()

}

function draw() {
  background(255, 0, 150);
  //heart
  strokeWeight(0)
  fill(255, 0, 0)
  ellipse(200, 200, 250, 250)
  ellipse(400, 200, 250, 250)
  triangle(110, 290, 300, 490, 490, 290)
  ellipse(300, 300, 100, 100)
  //text
  fill(0)
  textStyle(BOLD);
  textSize(20)
  textAlign(CENTER, TOP)
  text('It has been ' + months + ' months and ' + days + ' days since \n we first started dating', 300, 250)
}

My problem is that after the new year or any month change the dates will go into the negatives.
How would I make it so it keeps track of today's date and the set date and tells me the time in between them?

Ann Kilzer
  • 1,266
  • 3
  • 16
  • 39
IlikeVisa
  • 1
  • 2
  • How would you figure this out without a computer? If I gave you two dates, how would you calculate how many days were between them? – Kevin Workman Dec 18 '19 at 16:55
  • Subtraction, but different months have a different amount of days in them. When the date is changed to 2020 or a new month it displays a negative. I don’t know how to keep track of the of the amount of days and months that have passed without it going to a negative or losing all its data in a new year. – IlikeVisa Dec 19 '19 at 12:04
  • How do you keep track of that stuff when you're doing this by paper? Taking a step back, you need to [break your problem down into smaller steps](https://happycoding.io/tutorials/how-to/program). Maybe step 1 would be creating a variable that holds how many days are in each month? – Kevin Workman Dec 19 '19 at 17:05

1 Answers1

0

I'll point you in the right direction.

JavaScript has a Date object you can use:

let anniversary;

function setup() {
  // Our anniversary is 4/7/2019
  createCanvas(600, 600);
  anniversary = new Date(2019, 7, 4); // initialise your anniversary

You can also get the current date by simply newing up a new Date()

let currentDate = new Date();

You could use this to calculate the difference between the two dates, there's endless resources for this. Here's one to get you going!

Luke Garrigan
  • 4,571
  • 1
  • 21
  • 29
  • ```let anniversary; function setup() { // Our anniversary is 4/7/2019 createCanvas(600, 600); anniversary = new Date(2019, 7, 4); // initialise your anniversary } function draw() { background(213) text(anniversary, 10, 10) }``` It prints the variable but how do I remove the extra junk and extract only the data I want, like the month and make that a variable that way I can turn the data into a sentence. – IlikeVisa Dec 19 '19 at 21:54
  • Try `anniversary.getMonth()`. check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date for a comprehensive list of all the methods. – Luke Garrigan Dec 20 '19 at 10:35