Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Javascript - calculate time passed while offline? (plus event occurring at specific time)

Currently, I'm writing a small program for my own personal use. So far, everything is working as expected. However, a specific problem is arising that I have no idea how to solve. I need to accomplish three things: (1) find the day a specific button is pressed and the program is started, (2) how many days have passed from now until then, and (3) know this even if the program itself has been closed in the meantime. The first two seem irritating, as I must deal with this bizarre variable which counts up in milliseconds the time right now based on midnight of January 1st, 1970, but not impossible if I were to work on it for some time. The third is where the real problem lies - I would imagine I have to save the information in the form of a cookie but I haven't the slightest idea how to do that or what resources would be needed. I tried to look it up but I couldn't quite figure out what I needed to do - it seemed like I was missing some piece of information I would need.

In addition to this, if there's a simpler way to calculate the date, that information would also be very helpful. Thanks!

1 Answer

is this on a website? you could use local storage to save the time of the button click event. (2) can be derived from that. there is a library called moment.js that can help convert epoch times into human readable dates/times.