Session storage - 01/01/1970 by default

Hey!

When I use a date picker with session storage, it sets the date to 01/01/1970 as a default year if the user has not picked a date. I want the default to be today’s date or empty.

I’m using this code:

import { session } from 'wix-storage';

$w.onReady(function () {
    $w('#input1').value = new Date(session.getItem("date"));

});

export function input1_change(event) {
 var date = $w('#input1').value
    session.setItem("date", date);
}

Thank you,
Mari

In the onReady() function, you are setting the Date directly from the memory. If the memory hasn’t been saved previously, then you have an undefined value which will result in the Date being undefined - or 01/01/1970.

You need to do something like this:

$w.onReady(function () {
    let dt = session.getItem("date");
    if(dt === undefined || dt === null) {
        $w('#input1').value = new Date();
    }
    else {
        $w('#input1').value = new Date(session.getItem("date"));
    }
});

Thanks Yisrael, works great!