Randomize DateTime once before storage into mongodB collection ( singleton ) using momentJS


#1

Is there a way to read a set of DateTime objects from JSON and randomize DateTime JUST ONCE the minutes (i.e. singleton) before storage into mongodB

{ start_date : 2016-09-12 12:15:00 }

-> randomize just once +/- 15mins to something like

{ start_date: 2016-09-12 12:08:00 }

etc

Because I am doing the read in a CRON job, hence the need for a singleton, but from what I read the usual implementation uses classes? Can I do without classes in ES6 ?


#2

Have you considered using a library like moment to handle this? For example:

import moment from 'moment';

const timestamp = moment('2016-09-12 12:15:00');
const multiplier = Math.round(Math.random()) ? 1 : -1;
timestamp.add((multiplier * Math.floor(Math.random() * 15)), 'minutes');
console.log(timestamp.toDate());

#3

Hi Huge

Any idea how to refactor this into a singleton in ES7 using static?

function randomizeStartDateTime(record) {

    const timestamp = moment(record);
    const multiplier = Math.round(Math.random()) ? 1 : -1;
    timestamp.subtract((multiplier * Math.floor(Math.random() * 15)), 'minutes'); 

    return timestamp.format("YYYY-MM-DD HH:mm:ss");
}
class classRandomizeStartDateTime {

static instance;

constructor(record) {
    if (classRandomizeStartDateTime.instance) {
    return classRandomizeStartDateTime.instance;
}

const timestamp = moment(record);
const multiplier = Math.round(Math.random()) ? 1 : -1;
timestamp.subtract((multiplier * Math.floor(Math.random() * 15)), 'minutes'); //15
console.log(timestamp.format("YYYY-MM-DD HH:mm:ss"));
classRandomizeStartDateTime.instance = timestamp.format("YYYY-MM-DD HH:mm:ss");
    }
}

console.log('classRandomizeStartDateTime output is : ', new classRandomizeStartDateTime() === new classRandomizeStartDateTime());

But this returns ‘false’

Should be:

class classRandomizeStartDateTime {

    static instance;

    constructor(record) {
        if (classRandomizeStartDateTime.instance) {
        return classRandomizeStartDateTime.instance;
    }

    const timestamp = moment(record);
    const multiplier = Math.round(Math.random()) ? 1 : -1;
    timestamp.subtract((multiplier * Math.floor(Math.random() * 15)), 'minutes'); //15

    console.log('classRandomizeStartDateTime is : ',timestamp.format("YYYY-MM-DD HH:mm:ss")); // classRandomizeStartDateTime is :  2016-09-14 11:45:53
    // classRandomizeStartDateTime is :  2016-09-12 12:27:00
    // classRandomizeStartDateTime is :  2016-09-12 12:12:00

    this.state = timestamp.format("YYYY-MM-DD HH:mm:ss");
    classRandomizeStartDateTime.instance = this;
    }
}

    console.log('classRandomizeStartDateTime output is : ', new classRandomizeStartDateTime('2016-09-12 12:15:00') === new classRandomizeStartDateTime('2016-09-12 12:15:00'));

    let time1 = new classRandomizeStartDateTime('2016-09-12 12:15:00');
    let time2 = new classRandomizeStartDateTime('2016-09-12 12:15:00');

    console.log('Time1 is :', time1);
    console.log('Time2 is :', time2);