Error: Calculation of date 30 days in the past incorrect

Currently, I am in the process of developing a function that compares the current date with an expiration date. The input, expireStamp, is represented as a timestamp measured in milliseconds.

compDate = function(expireStamp) {
    // Convert the timestamp of expireStamp to a readable Date object
    var expireDate = new Date(expireStamp);
    var notifyDate = new Date().setDate(expireDate.getDate() - 30);
    var today = new Date(); // Current date

    console.log("Today: " + today);
    console.log("Notify: " + new Date(notifyDate));
    console.log("Expire: " + expireDate);

    if(today.getTime() <= notifyDate) {
        // The date is still valid
        return "good";
    } else {
        // The date may be expired
        if(today.getTime() > notifyDate && today.getTime() <= expireDate.getTime()) {
            // The date is soon to expire
            return "soon";
        } else if(today.getTime() > expireDate.getTime()){
            // The date has already expired
            return "fail";
        }
    }
}

In this scenario, we need to compare today's date against two dates: the expiration date and the notification date. The notification date is set as 30 days before the expiration date. The issue I am facing revolves around the notification date calculation. If the expiration date is set too far into the future, the notification date behaves unexpectedly. Here are some test cases:

> var exp = new Date(1409362782000)
undefined
> exp
Fri Aug 29 2014 21:39:42 GMT-0400 (Eastern Daylight Time)
> var notify = new Date().setDate(exp.getDate() - 30);
undefined
> notify
1396183229815
> var test = new Date(notify);
undefined
> test
Sun Mar 30 2014 08:40:29 GMT-0400 (Eastern Daylight Time)

I have set the expiration date as August 29th (considering today is April 4, 2014) using a timestamp in milliseconds. As you can see, exp is displaying the correct expected date.

The issue arises when calculating the Notify date which should ideally be 30 days prior to exp. However, notify returns March 30th, which is significantly more than 30 days before August 29th. This problem occurs mainly with dates further in the future compared to the current date.

To resolve this problem, I aim to ensure that the notify date accurately reflects being 30 days before the expiration date.

Answer №1

> start = new Date(1409362782000)
Sat Aug 30 2014 05:39:42 GMT+0400 (MSK)
> end = new Date(start.getTime() - (30 * 24 * 60 * 60 * 1000))
Thu Jul 31 2014 05:39:42 GMT+0400 (MSK)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The functionality does not seem to be functioning in Mozilla Firefox, however it is working correctly in Chrome when the following code is executed: `$('input[data-type="choise"

I am currently working on an online test portal and everything is functioning properly with Chrome. However, I am encountering an issue with Mozilla Firefox. My code works fine in Chrome but not in Mozilla Firefox. Please suggest an alternative solution to ...

Error: PHP is showing an undefined index error when trying to decode JSON, even though the value

I am feeling quite puzzled. I transferred a key value pair object from jQuery to PHP and successfully alerted it back out. However, when I visit the PHP page, it indicates that the data is either null or an undefined index. Here is my jQuery code: $(&ap ...

Are there limitations to consider for the 'onDeviceMotion' event on iOS systems?

Encountering an issue with accessing the onDeviceMotion event on iOS but not Android. After successfully implementing code from a tutorial on my iOS device, I found that running the exact same code from localhost does not trigger the onDeviceMotion event. ...

Retrieve the class using a text string and then obtain the subsequent string

I am searching for the first class containing the text "Routed:" and then I want to retrieve the following string. I attempted to utilize document.querySelector, but I'm uncertain if this is the correct approach. <td class="msvb2">Routed:</t ...

Switching between different CSS files based on the URL using jQuery or another method

Is it feasible to apply specific styles based on the ID or load various CSS files depending on the URL you are visiting? For example: <script> if(location.href == 'http://jpftest2.tumblr.com/about'){ document.write('<style type= ...

ways to display nested arrays in angularjs

I'm having an issue with binding and displaying the value of an array. When I assign the value using the scope variable like this: $scope.StockList = obj.data Everything works fine. However, when I try to push the value into the array like this $ ...

Unable to access a variable from a MongoDB query that is being awaited

Having an issue utilizing the mongodb query result in another query. Here's some code for reference (inside an async function) - Pay attention to how I'm using created_comment._id in the second query: let created_comment = await Comment.create(n ...

What is the best way to conceal a button before printing and have it reappear once the printing process is finished?

I have a button on my webpage with the id "print_req" that is used to trigger a JavaScript function for printing the page. I want this button to be hidden during the printing process and then reappear afterwards, so it does not show up in the printed docum ...

Modifying the CSS properties of elements with identical IDs

Encountering an issue while attempting to modify the CSS property of elements with similar IDs using JavaScript. Currently, running a PHP loop through hidden elements with IDs such as imgstat_1, imgstat_2, and so forth. Here is the code snippet being emp ...

Utilizing a JavaScript variable to fetch a rails URL: A comprehensive guide

One interesting feature I have is an image link that has a unique appearance: <a href="#user-image-modal" data-toggle="modal" data-id="<%= image.id %>"><img class="user-photo" src="<%= image.picture.medium.url %>" alt="" /></a&g ...

Determining the total of input values based on identification number

Can anyone help me figure out how to calculate the total sum of the values entered using IDs? I've been struggling with it and can't seem to get it to work. Your assistance would be greatly appreciated. <input type="number" id=&quo ...

Restricting the rotation of a camera in Three.js

Within my Three.js project, there is a 3D scene featuring a floor and an orthographic camera. I have successfully enabled the user to navigate the scene using their mouse. However, I am facing a challenge in restricting the camera's movement within t ...

What is the process for developing a personalized search feature in VueJS?

How can I create a custom search function in VueJS that allows me to input partial product names and receive relevant results? For example, I have 2 products named PlayStation Plus 90 Days IE and PlayStation Plus 90 Days NO. Currently, I have to input the ...

Implementing a color filter on a camera using Three.js

Can a parameter be adjusted for the camera or renderer to apply a tint to everything on the stage, such as a red shade resembling a "glasses effect", without having to manually modify each object's materials? ...

Tips for Retrieving the Key Names of JSON Objects within a JSON Array

I'm trying to retrieve the object names "channelA" and "channelB". Here is the JSON data: [ { "channelA": { "programmes": [ { "start_utc": 1522208700, "stop_utc": 152220 ...

Utilizing a jQuery plugin or function with customizable options when adding elements dynamically

jQuery offers a useful feature where you can use $(document).on(...) to apply an event to newly added elements in the HTML, such as after an Ajax request. Can you provide an example of how to use this with a custom function/plugin like hoverpulse? Anothe ...

What is the method for displaying the value of a textarea?

I am relatively new to the world of coding, but I have already delved into HTML, CSS, and basic DOM scripting. My goal is simple - I want to create a comment box where people can leave messages like in a guestbook. However, when I input text and click on ...

sending a file to a controller without the use of a form

Hey there, I'm exploring a way to transfer a file from a view to a controller without using a form. My goal is to enable the user to browse for a file and have it sent to the controller via Ajax. Do you think this is achievable? <td>Import ...

I am excited to create a Dynamic Routing system that selects specific elements from an array of objects using Typescript

1. crops-list.component.ts import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-crops-list', templateUrl: './crops-list.component.html' ...

Is there a way to incorporate locales in calculations involving percentages?

Given the number 4030.146852312 I want to retrieve the four decimal places from this number, resulting in: 4030.1468 Furthermore, I need to format this number according to the specified locale. For example: 4.030,1468 What is the best way to achieve thi ...