I have a list of dates with properties and method I create with :
New CalendarDate('2018-XX-XX')
And for each date, I have a list of hours. I would like each of my hour to reference one single date.
At the moment, the solution I came from seems... inadequate, and I would like to understand the proper way to do it. So for each hour I create a new instance of Hour and pass the newly created date. I.E :
Class CalendarDate {
dateString: string
constructor(date: string) {
this.dateString = date
}
getDate() {
return this.dateString
}
}
Class CalendarHour {
date: CalendarDate
constructor(date: CalendarDate) {
this.date = date
}
}
let date1 = new CalendarDate('2018-11-11')
let hour1 = new CalendarHour(date1)
let hour2 = new CalendarHour(date1)
I want to avoid creating a new CalendarDate for every CalendarHour, but I'd rather like each CalendarHour to refer to the same CalendarDate object. What would be a better approach than the one I came up with ?
Related
I'm trying to work with some data that has close bindings to dates. As shown in the snippet down below I'm trying to find the index of the closest day to today. I'm using date-fns utility library for this job. While I'm trying to log the closestIndex from it works fine and got an output in the terminal but while I'm trying to utilize the value of closestIndex I got an error that says closestIndex is undefined.
Any Ideas would be very appreciated.
import * as dateFns from 'date-fns';
const today = new Date().getTime();
const dates = [
2022-04-10T14:07:12.276Z,
2022-04-10T14:07:06.967Z,
2022-04-10T14:07:04.663Z,
2022-04-10T14:07:03.040Z,
2022-04-10T14:07:01.420Z,
2022-04-10T14:06:59.869Z,
2022-04-10T14:06:53.223Z
]
const closestIndex = dateFns.closestTo(today, dates);
console.log(closestIndex); // => 0
console.log(dates[closestIndex]); // => undefined could not be used as index value
You should use real Date objects inside your array (rather than ISO-8601 values which are not even quoted) and use closestIndexTo instead of closestTo (which will return the Date value itself rather than its index in the array)
const today = new Date().getTime();
const dates = [
new Date('2022-04-10T14:07:12.276Z'),
new Date('2022-04-10T14:07:06.967Z'),
new Date('2022-04-10T14:07:04.663Z'),
new Date('2022-04-10T14:07:03.040Z'),
new Date('2022-04-10T14:07:01.420Z'),
new Date('2022-04-10T14:06:59.869Z'),
new Date('2022-04-10T14:06:53.223Z')
]
const closestIndex = dateFns.closestIndexTo(today, dates);
console.log(closestIndex); // => 0
console.log(dates[closestIndex]); // => "2022-04-10T14:07:12.276Z"
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js"></script>
I need to initiate lots of relational objects, the lifecycle is quite long and the amount keeps growing.
I finally started a deep dive into references and I hope this is where I could get a big win (both in saving memory an avoiding huge garbage collection spikes).
Does it make sense to pre initiate objects and use refs instead of creating new each time?
Simple example:
Person object which has a property birthDate: {day, month}
30 (days) * 12 (months) = 360 possible objects if pre initiated
1000 people would already create 1000 new date objects
I assume it would save a lot of (well, that's relative) memory in this case? Am I correct?
// would this make sense if the # of Persons is so high that
// the probability of all dates being used is close to 100%?
class BirthDate {
constructor (props) {
this.day = props.day;
this.month = props.month;
}
// would also be nice to add methods, e.g:
getAge (currentDateTime) { /* .. */ }
}
let dates = {
'3.7': new BirthDate({day: 3, month: 7}),
'4.7': new BirthDate({day: 4, month: 7})
// etc, 1-30 days for 1-12 months
};
class Person {
constructor (props) {
this.id = props.id;
this.birthDate = props.birthDate;
}
}
let people = { lookup: {}, array: [] };
for (let i = 0; i < 1000; i++) {
const person = new Person({
id: `whatever-${i}`,
birthDate: {day: 3, month: 7}, // <- new location in memory each time, lots of duplicates
// birthDate: dates[`${3}.${7}`] // <- should use only reference right?
});
people.lookup[person.id] = person;
people.array.push(person);
}
console.log(people);
The answer is yes, you can have huge gains this way in terms of storage and ultimately this affects performance as well. But there is a catch. If you have the same birthDate for many people and you need to edit a birthDate, then changing some attributes of the birthDate will effectually change the birthDate everyone else having the same reference. So, in my opinion the appropriate approach would be to store birthdates separately in an easy-to search manner, like:
{
//Year
'1985': {
//Month
'07': {'26': {/*Some members*/}}
}
}
and write some functions which enable you to search/add/edit/remove values, so, if you are to change someone's birthDate, you will just search for that birthDate reference in this object above. If not found, then created, so you will end up with an actual birthDate, which you can assign to the person as the edited birtDate, not affecting other people if not necessary.
In MongoDB collection I have 3 objects. I need to update one variable (date type) in each object. The main task is to increment the date of the objects. For example: all objects have the same variable:
"Time1" : ISODate("2016-01-12T21:37:46.738Z")
My problem is to update the first object with the current date, manually I do it in this way:
$db.getCollection('my.data')({'_id':ObjectId("52e637fca92cf1ec6a73c1e8")}, {$currentDate: {Time1: true}})
The next is to increase the date of the second object by 1 day, I mean to update it with tomorrow date. I couldn't do it through the shell, because $inc doesn't work with Date type.
So, I am lost with javascript
I found how to get it with java script but I don't know how to collect all if this in one script.
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);
Thank you for your help.
You could use the $set operator for the other fields, together the the $currentDate operator all within the update object:
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);
var dayAfterTomorrow = new Date();
dayAfterTomorrow.setDate(dayAfterTomorrow.getDate()+2);
db.getCollection("my.data").update(
{ "_id": ObjectId("52e637fca92cf1ec6a73c1e8") },
{
"$currentDate": { "Time1": true },
"$set": {
"Time2": tomorrow,
"Time3": dayAfterTomorrow
}
}
)
I have an array of Javascript datetime objects which I want to display on a cal-heatmap. For this, i've done the following:
var startTimes = [] //array of datetimes to show
var cal = new CalHeatMap();
cal.init({
itemSelector: '.heat-map',
domain: 'day',
subDomain: 'hour',
range: range,
start: new Date(startTimes[0]),
highlight: new Date(startTimes[0])
});
for (s in startTimes) {
cal.highlight(cal.options.highlight.push(new Date(startTimes[s])));
}
This however, doesn't seem to work as only the first date gets marked.
Note that push returns the new length of the array and not the element you just added to it.
According to this and the doc of cal-heatmap/#highlight which states that the method expects a date, I guess your code should be:
for (s in startTimes) {
var date = new Date(startTimes[s]);
cal.options.highlight.push(date);
cal.highlight(date);
}
How can I select the features from the property attributes of the feature ?
Have a look at Array.protoype.filter()
Ok, it's not pretty but it works;)
Create a filter function
function filterBy(element) {
return
element.properties.systemtime_start ==
getMonthFromEpoch(element.properties.systemtime_start);
}
Create a function to filter the month
function getMonthFromEpoch(time) {
var month = 'noMonthSet';
var tMonth = new Date(time).toLocaleDateString('en-US', {month: 'long'});
if (tMonth == 'January') {
month = time;
}
return month
}
Pass the part with the features of your object to the filter
var filterd = yourObject.features.filter(filterBy)
This will return a new array with objects matching to systemstartTime 'January'
One more thing. I would change your naming. Use system_time_start instead of system:time_start. Otherwise you won't be able this property via JS