Store Date and Retrieve from Local Storage

11,257

Solution 1

You can get it back as unix timestamp. Make sure to pass a number to Date constructor.

First, save. Add a + to new, to make this a timestamp.

localStorage.setItem('time', +new Date);

Then later retreive it, but pass the number to Date constructor:

new Date(parseInt(localStorage.getItem('time')));

Solution 2

Store the UNIX timestamp and then recreate the date object from that:

window.localStorage.time = new Date().getTime();

var date = new Date(parseInt(window.localStorage.time));
Share:
11,257
Joseph Astrahan
Author by

Joseph Astrahan

Age: 32 Programming Experience: 20+ Years ( Since I was 12 years old ) Below is the technology stack I have moderate to expert level experience with: Mac OS X & iOS Programmer COCOA/Objective-C/Swift 3 C/C++ RealBasic / Xojo (Crossplatform Linux, Windows, Mac Language) PHP 7+ MySQL, PostGreSQL Server Setup Linux - Apache2 - PHP - .htaccess File Customization/Manipulation Javascript / jQuery / jQueryUI / KendoUI AJAX Techniques Regular Expressions HTML5 & CSS3 (Local Storage, Camera + Video + More) Docker, Docker-Compose, Docker-Swarm (LAMP Setup) Amazon Web Services (S3, Elastic Bean Stalk, IAM, DynamoDB) Xojo & Appcelerator (cross platform tools for mobile android & iOS) PHP Frameworks (Symfony Framework, CodeIgniter & SLIM 3) Twig Templating Engine Doctrine 2 Database Abstraction Layer (can automate creation from workbench file) Responsive Web Design React (Components) & Redux JS Libraries React Native (For Mobile -- android, iOS -- development under javascript) Twilio API Google Maps & Email API Digital Ocean API & Load Balancers/Monitoring Experience Jenkins (Continuous Integration tool of choice) Selenium Web Driver (For web application testing) JMeter ( load balancing testing ) PHPUnit & Behat Library for use with Selenium for testing Shell scripting or Ansible for Deployment (Continuous Deployment tools) Microsoft Visual Studio for Git Repo & Scrum Tool Github & Bitbucket Git Repo Experience Wunderlist Scrum Tool Experience Vue & React Frontend Experience Springboot Backend Experience Please contact me anytime if you are hiring as I'm currently looking for jobs. My email is [email protected]

Updated on June 14, 2022

Comments

  • Joseph Astrahan
    Joseph Astrahan almost 2 years

    I stored a date to local storage like below.

    JS:

    var currentTime = new Date(); //get the current time.
    
    //Clock in the time.
    localStorage.time = currentTime;
    

    When I try to retrieve it sometime later using...

    var timeObj = new Date(localStorage.time);
    var checkInDayOfMonth = timeObj.getUTCDate(); //returns 1-31 
    

    the timeObj will not have the correct datetime, it instead appears to have the current time as if it's ignoring the parameters of the time I'm sending.

    I'm using getUTCDate to get the day of the month. If the value of today is different than what is in storage I know it's a new day.

    Opening Google Chrome Inspector reveals the date stored in localStorage in this format:

    Wed Dec 11 2013 22:17:45 GMT-0800 (PST)
    

    Is that not an acceptable format for the date constructor?

    How can I store and retreive dates from localStorage correctly?

  • Joseph Astrahan
    Joseph Astrahan over 10 years
    I tried doing this but I just get back NaN. I see the UNIX timestamp (the long series of numbers). It just won't seem to create the date object from it correctly. Any ideas? Did you actually test this idea?
  • Joseph Astrahan
    Joseph Astrahan over 10 years
    I think my comment came out looking wrong, I meant to say I really like this idea (if it will work). Sometimes people post pseudo code. So was curious if that is what you were doing or if you have tried this idea and it verifiably worked?
  • Joseph Astrahan
    Joseph Astrahan over 10 years
    The above has an error and does the current day before month, also month you need to say+1 since it returns 0-11
  • Zlatko
    Zlatko over 10 years
    The retreival part needs a parseInt. Look at my answer?
  • wizzwizz4
    wizzwizz4 about 6 years
    @JosephAstrahan Many people wouldn't consider that to be an error; if you're being picky, it should be YYYY-MM-DD.
  • Joseph Astrahan
    Joseph Astrahan about 6 years
    I wrote that comment years ago lol, I agree with what you said with my knowledge now