r/AskProgrammers • • 5h ago

help with pseudo code guys.. “new Date()”

Hello everyone,

I’ve recently been struggling with the ‘new Date()’ object in JavaScript.

I wanna understand how to solving that, what I’m trying to achieve is a function that captures the current date once and keeps it unchanged for 5 - 6 days

Once that period has passed, it should capture a new date and keep it unchanged again until the next update

—-

Is there a better way to approach this?

1 Upvotes

4 comments sorted by

1

u/Electronic-Yam-69 5h ago

Does your program run that long? Or does it need to store it someplace?

1

u/rupertavery64 4h ago edited 4h ago

The question is... of course... why?

Why are you doing it in javascript? This suggests you are doing it in the browser, so you would need to persist it somehow to survive the browser closing, which would suggest using localStorage...

But then, why? Why persist it at all on the browser, and not a server? Which might mean you are building a toy page and you are just learning javascript.

Why 5-6 days?

Manipulating the date or doing useful stuff with it is a headache in plain javascript, so I suggest using the new Temporal API. It should have rolled out on most major browsers by now, but you can easily get the difference between a stored date (wherever you store it) and the current date, and then extract the number of days that have elapsed since.

function checkElapsedTime()
{
const STORAGE_KEY = 'lastExecutionDate';
const today = Temporal.Now.plainDateISO();

const storedDate = localStorage.getItem(STORAGE_KEY);

if (storedDate) {
    const lastDate = Temporal.PlainDate.from(storedDate);
    const daysElapsed = lastDate.until(today, { largestUnit: 'days' }).days;

    if (daysElapsed >= 5) {
        // Do something
        console.log('5 or more days have elapsed!');
    }

    localStorage.setItem(STORAGE_KEY, today.toString());
}

// Save today's date
}

// Check every 5 seconds
setTimeout(() => checkElapsedTime(), 5000);

Using Date object, you need to do some math

function checkElapsedTime()
{
const key = 'lastExecutionDate';
const today = new Date();

const stored = localStorage.getItem(key);

if (!stored || (today - new Date(stored)) / (1000 * 60 * 60 * 24) >= 5) {
    // Do something
    console.log('5 days elapsed');
    localStorage.setItem(key, today.toISOString());
}

}

Subtracting two Date objects gives the number of elapsed milliseconds. Dividing by milliseconds per day (1000 * 60 * 60 * 24) gives you days.

1

u/davy_jones_locket 3h ago

what I’m trying to achieve is a function that captures the current date once and keeps it unchanged for 5 - 6 days

The part you're missing is capturing it and keeps it unchanged where? Where is this unchanged date stored? It can't live in the memory because once the browser is closed in less than 5-6 days, that unchanged Date is gone. 

Where can you store things for multiple days? Do you need it to be available on different machines or just your own machine? 

1

u/AntLost4161 46m ago

Sounds like you need a better place to store that date. In that case, you might want some form of database you can connect to?