Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:14:02+00:00 2026-06-07T15:14:02+00:00

I am trying to calculate the difference between two times using JavaScript. It’s just

  • 0

I am trying to calculate the difference between two times using JavaScript. It’s just basic math but I seem to have some issues with that while using JSON.stringify() and JSON.parse().

If you’re wondering why am I applying the JSON.stringify() function to the date, it’s because I using local storage to store some data on the client side and use it whenever the client lands on my website again ( it’s faster that way rather than making more requests to the server ). That data usually updates once in a while ( I’m grabbing the data through API from another website ), so I set up a data_update variable and I’m storing it together with the other data.

That way I’m grabbing the stored data from the local storage and check if the difference between data_update ( which is a date / time ) and the time / date when the check it’s made and see if it’s greater than a week / day /etc .

So that is the reason why I’m using the JSON functions. My problem is that when I’m parsing the data from the local storage, the date seems to be different from a Date() object.

I’m trying to do the next operation per say :

var x = JSON.parse(JSON.stringify(new Date()));

var y = JSON.parse(this.get_local_storage_data(this.data_cache_key)); // the data object stored on local storage

var q = y.data_update; // this is the variable where the Date() was stored

console.log(Math.floor((x-q)/1000));

The above will return null. Also when I want to see the Math.floor(x) result, it returns null again.

So what can I do in this situation ? Is there a fix for this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:14:04+00:00Added an answer on June 7, 2026 at 3:14 pm

    If you look at the output of JSON.stringify for a Date, you’ll see that:

    JSON.stringify(new Date())
    

    Results in a string. JSON does not have a primitive representation of Date objects that JSON.parse will turn back into a Date object automatically.

    The Date object’s constructor can take a date string, so you can turn those string values back into dates by doing:

    var x = new Date(JSON.parse(JSON.stringify(new Date())));
    

    Then the arithmetic will work.

    x = new Date(JSON.parse(JSON.stringify(new Date())))
    y = new Date(JSON.parse(JSON.stringify(new Date())))
    y - x
    => 982
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to calculate difference(in seconds) between two date/times formatted as following: 2010-05-11
Possible Duplicate: how to calculate difference between two dates using java I'm trying something
I am trying to calculate the difference between given two times and i find
I am trying to calculate the difference between two dates. This is what I'm
I want to calculate the difference between two dates in php. I am trying
I am trying to calculate daysbetween difference between two dates in the grid. This
I'm trying to calculate the smallest difference between two angles. This is my current
I am trying to calculate difference between two date strings both of which are
I am trying to calculate the width and difference between the widths of a
I have a train schedule app, and I am trying to calculate the difference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.