Estimating Dates Using Javascript and JSON

I am currently working on visualizing some data I have. You can view the fiddle here: http://jsfiddle.net/hohenheim/6R7mu/10/

As part of this visualization, I have extracted a small subset of the JSON data that I am trying to represent, named data2. Here is what it looks like:

data2 = [
   {
      "startDate":1396263600.0,
      "adId":2483231759355,
      "endDate":1401101940.0,
      "impressions":754831
   },
   {
      "startDate":1393851600.0,
      "adId":2750329551133,
      "endDate":1404212340.0,
      "impressions":3947368
   }
];

This data includes date ranges. My objective is to evenly distribute the impressions across all days within each date range and then aggregate the total impressions per day across all ads. Therefore, the final outcome should only consist of 2 attributes: date and impressions. Although I have a proposed solution, there seems to be an issue with aggregating the days correctly because the millisecond representations of certain dates are not identical, even though they fall on the same day. This discrepancy impacts the accuracy of my calculations.

If you examine the chart closely, particularly in the central area, you may notice fluctuating values. In reality, each pair of varying values should actually correspond to a single day when combined.

Is there a method to effectively combine two different versions of milliseconds representing dates that essentially refer to the same day?

Answer №1

Most timekeeping devices do not account for leap seconds, allowing you to extract the day component by subtracting

(t % (24     /* hours in a day */
      * 3600 /* seconds in an hour */
      * 1000 /* milliseconds in a second */ ))

from the timestamp t.

Even with leap seconds included in the timestamp, this method can still provide a reliable approximation of the day for comparison purposes. However, it may be less accurate near midnight.

For those located in distant time zones, it is advisable to adjust the timestamp to milliseconds since the epoch in the local time zone and disregard Daylight Saving Time adjustments, which could introduce some error around midnight.

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

Utilizing an imported variable beyond a component function within React useEffect: A comprehensive guide

I'm encountering a problem while trying to utilize the imported variable likedImages within a useEffect hook in a React component. When I include likedImages in the dependency array, I receive a warning indicating that it is an unnecessary dependency ...

How to Deactivate the Default Selection in React-Select

Having trouble with the focus in a React Select dropdown. The first item always gets focused when opening the dropdown, despite passing various props without success. I checked their GitHub for related issues around autofocus but couldn't find a solut ...

What is the Vue.js alternative to using appendChild for dynamically inserting new elements or components?

When working with Vue.js, I have successfully created a var app = new Vue({...}); and defined a component Vue.component('mycomponent', .... I am able to use this component by directly adding <mycomponent></mycomponent> in my HTML. How ...

A checkbox placed within an anchor tag

Here is some code that I have: <a href class="dropdown-toggle"> <input type="checkbox" ng-model="test.checked"> <span class="fa fa-angle-down"></span> </a> When I click on the above code, I want the chec ...

Receiving encoded characters in the response

URL: I have encountered an issue where I am trying to retrieve the PDF file from the URL above using code. In tools like Postman or Insomnia, I am able to see the output as expected in PDF format. However, when I attempt it with code, I am receiving rando ...

Troubleshooting the issue of AJAX not successfully transmitting variables to PHP

let xmlhttpRequest; if (window.XMLHttpRequest) { xmlhttpRequest = new XMLHttpRequest(); } else { xmlhttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttpRequest.open("POST", "test.php", true); xmlhttpRequest.setRequestHeader("Content-typ ...

Creating a Vue.js v-for loop to dynamically display a series of DIVs in segments

Here is the code I am currently working with: <div class="container-fluid" id="networdapp" style="display:none;"> <div class="row" > <div v-for="(result,i) in results" :key="i" class="col-sm-6" > <div class=" ...

Navigating through nested data in React TypeScript can be accomplished by accessing the nested data

How can data in a nested interface like the one shown below be accessed in React TypeScript? export interface School { prices: Array<{ state: { response_header?: { school_type_opportunities?: Array<{ benefit_type_opportunity?: st ...

Having trouble retrieving the default selected value using the index in Angular Material's mat-button-toggle functionality

I'm encountering an issue with setting the default value for a toggle button group. The code is simple and the toggle buttons are correctly fetching values from the index, but I can't seem to get one of them to be default selected. I tried settin ...

Tips on adding style to your jQuery autocomplete dropdown

I am currently utilizing jQuery autocomplete functionality. I have configured it to communicate with a service and retrieve records: <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1 ...

What are the steps for incorporating React into an existing website?

Currently, I have a website built with Express and I am looking to incorporate React into specific pages. What is the most effective method to integrate it while utilizing all the resources available through create-react-app? I understand that I can manua ...

There was an issue encountered while attempting to execute the command: npx create-next-app -e with-tailwindcss my-project

Every time I attempt to set up a new Next.js Tailwind 'App' using the command npx create-next-app -e with-tailwindcss my-project, I encounter the following issue: C:\socialmedia3>npx create-next-app -e with-tailwindcss socialmedia3 Creati ...

Ways to transmit information among Vue components

I am trying to figure out how to pass data from the Module variable in CoreMods.vue to ExternalWebpage.vue using Vuex. I want the CoreModule state to be watched for changes in the external webpage. This is my store.js setup: import Vue from 'vue ...

Implementing JavaScript for showcasing weights

I've encountered a JavaScript function that modifies user preferences for weight units, allowing them to choose between metric and imperial measurements. When displaying weights on my page, I typically present them as follows: This is a brief explan ...

How can I obtain the model values for all cars in the primary object?

const vehicles={ vehicle1:{ brand:"Suzuki", model:565, price:1200 }, vehicle2:{ brand:"Hyundai", model:567, price:1300 }, vehicle3:{ brand:"Toyota", model ...

Error: [$controller:ctrlreg] - The controller registration has failed

I am currently exploring the world of AngularJs and attempting to display options from a json file. However, I keep encountering the following error message: "Error: [$controller:ctrlreg]" Below is the code snippet I am working with: var sj = angular. ...

Load blender scene with functional lighting using THREE.JS GLTFLoader

Recently, I've been experimenting with the GLTFLoader feature in THREE.js along with the new blender exporter found here. After successfully adding a model with similar color material and importing the scene into my THREE.js project, I noticed that wh ...

Mastering the Art of Mocking DOM Methods with Jest

What is the best way to simulate this code snippet using Jest : useEffect(() => { document .getElementById('firstname') ?.querySelector('input-field') ?.setAttribute('type', &apos ...

angular interpolation:issue arises when inserting URL stored in a variable

A challenge I'm facing involves adding a dynamic id to a YouTube URL, which looks something like this: <iframe width="460px" height="415px" ng-src="{{post.youtube_id}}" frameborder="0" allowfullscreen></iframe> One of the URLs I'm a ...

Guide on appending a file to a formData object in vue.js

Having trouble adding the file from the input to the formData object. Even after trying multiple solutions, the object appears to be empty when I log it. Can't seem to figure out what's wrong. File Input: <input class="btn btn-sm btn-rounded ...