Drupal 8: Bootstrap Sticky is not sticky in Drupal 8 - Solved
Bootstrap has a simple solution to have sticky blocks in your html. I’ve given a…
May 14, 2020
In your backend and frontend projects, you always need to deal with dates. Some examples are:
Writing plain javascript code is error prone, you need to be super sure of what you are doing. MomentJs is your savior. It is much popular in this domain. It provides lot of handy functions/methods which are very useful in date related arithmetics.
Lets take a look at some of common usages:
const moment = require('moment');
const currentDate = moment();
const moment = require('moment');
const now = moment(new Date().toISOString());
console.log(now);
var dates = [
moment('2017-12-30T11:36:51.992Z'),
moment('2017-12-29T11:36:51.992Z'),
moment('2017-12-28T11:36:51.992Z'),
moment('2017-12-27T11:36:51.992Z'),
moment('2017-12-26T11:36:51.992Z'),
moment('2017-12-25T11:36:51.992Z'),
];
for(let i=0; i<dates.length; i++) {
let dt = dates[i];
console.log('date', dt);
console.log('Days', now.diff(dt, 'days'));
console.log('hours', now.diff(dt, 'hours'));
console.log('minutes', now.diff(dt, 'minutes'));
}
Add 15 days to current date.
const moment = require('moment');
const now = moment();
console.log(now);
now.add(15, 'days');
console.log(now);
// Output
// moment("2020-05-20T14:01:29.933")
// moment("2020-06-04T14:01:29.933")
Note above that by calling add function, the object now is updated. You can assign it to another variable as well. But, both the objects will have same value. See below section for not updating calling object.
const moment = require('moment');
const now = moment();
console.log(now);
//add 15 days
const after = now.add(15, 'days');
console.log(now);
console.log(after);
// Output
// moment("2020-05-20T14:04:57.000")
// moment("2020-06-04T14:04:57.000")
// moment("2020-06-04T14:04:57.000")
Similarly you can add months and years to a date. By using string months, years.
Note: Its not like adding 30 days to current date.
const moment = require('moment');
const now = moment();
console.log(now);
//next month, same date
let after = now.add(1, 'months');
console.log(after);
//next month, same date
after = now.add(1, 'months');
console.log(after);
//next month, same date
after = now.add(1, 'months');
console.log(after);
// Output
// moment("2020-05-20T14:06:35.099")
// moment("2020-06-20T14:06:35.099")
// moment("2020-07-20T14:06:35.099")
// moment("2020-08-20T14:06:35.099")
const moment = require('moment');
const now = moment();
console.log(now);
//next month, same date
let after = now.subtract(1, 'months');
console.log(after);
// Output
// moment("2020-05-20T14:07:42.037")
// moment("2020-04-20T14:07:42.037")
const moment = require('moment');
const now = moment();
//next month, same date
let after = now.clone().add(1, 'weeks');
console.log(now);
console.log(after);
// Output
// moment("2020-05-20T14:11:27.766")
// moment("2020-05-27T14:11:27.766")
If you notice above, that the value of object now changed by calling add/subtract over the calling object. To avoid this, use clone()
const moment = require('moment');
const now = moment();
console.log(now);
//next month, same date
let after = now.clone().subtract(1, 'months');
console.log(now);
console.log(after);
// Output
moment("2020-05-20T14:10:10.061")
moment("2020-05-20T14:10:10.061")
moment("2020-04-20T14:10:10.061")
Bootstrap has a simple solution to have sticky blocks in your html. I’ve given a…
Problem Statement In a mysql table, I wanted to replace the hostname of the…
I was trying to install mongo extension with pecl. It gave me error: Then, I…
Introduction This post is about hosting ElasticSearch cluster on dockerised…
Read file all in one shot Above code read whole file at once, but if the files…
You might need to put sudo before above command. The command will show details…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…