Moment JS
Example of parsing and formatting a Date with Moment JS.
by Aswin Devarajan
HTML
<script src="http://momentjs.com/downloads/moment.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
JavaScript
let data = {
lastCounter: {
/* "penalty" : 0,
"plantId" : "client_2", */
block : 3,
/* "netdaydsm" : -5243346.11386679,
"netdaynetdsm" : -52433.4611386679,
"netdaygain" : -3450.11620612804,
"netdaycoalcost" : 48983.3449325399,
"netadddsm" : 0,
"netAdditionalPenalty" : 0,
"penaltyCounterUnderInjection" : 1,
"penaltyCounterOverInjection" : 0,
"overinjection" : 0,
"underinjection" : 1, */
date: "2019-04-07T13:03:16.455-06:30",
/* "createdAt" : ISODate("2019-04-07T13:03:16.456-06:30"),
"updatedAt" : ISODate("2019-04-07T13:03:16.456-06:30"),
"__v" : 0 */
}
}
// route to another function to find the gap till current block and calc the counters
let startTime = moment(data.lastCounter.date).startOf('day').add(15 * (data.lastCounter.block), 'm');
/* if (data.lastCounter.block === 96) {
startTime = moment(data.lastCounter.date).subtract(5, 'm').startOf('day').add(15 * (data.lastCounter.block), 'm');
} */
let currentTime = moment();
let diff = parseInt(currentTime.diff(startTime, 'm') / 15);
let blocksNumbers = Array.from({
length: diff
}, (_, k) => k + 1);
let blocks = blocksNumbers.map((o) => {
let blockCounter = data.lastCounter.block + o;
let block;
let dataDate;
console.log('blockcounter', blockCounter)
if (blockCounter <= 96) {
dataDate = startTime.unix();
block = blockCounter;
} else {
dataDate = moment(data.lastCounter.date).subtract(5, 'm').startOf('day').add(15 * (blockCounter), 'm').startOf('day').unix();
// dataDate = moment(data.lastCounter.date).startOf('day').unix()
let startOfDay = moment(data.lastCounter.date).subtract(5, 'm').startOf('day').add(15 * (blockCounter), 'm').startOf('day');
let blockTime = moment(data.lastCounter.date).startOf('day').add(15 * blockCounter, 'm');
...