moment().add() only works with literal values

Deprecated reverse overload add(unit: unitOfTime.DurationConstructor, amount: number|string) creates ambiguity. You can fix this by defining type of units to be DurationConstructor instead of string: let units: moment.unitOfTime.DurationConstructor=”month”; moment().add(1, units); Another option is just to use const instead of let, so literal type will be inferred: const units=”month”; moment().add(1, units);

How do I use momentsjs in Google Apps Script?

Most people try to use the library with the key ending in 48. That library is pretty dated (it is version 2.9 which is pretty old). Using eval and UrlFetchApp.fetch moment.js or any other external library can be used easily in google app scripts. function testMoment() { eval(UrlFetchApp.fetch(‘https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js’).getContentText()); var date = moment().format(“MMM Do YY”); Logger.log(date) … Read more

Array Of JS Dates How To Group By Days

Underscore has the _.groupBy function which should do exactly what you want: var groups = _.groupBy(occurences, function (date) { return moment(date).startOf(‘day’).format(); }); This will return an object where each key is a day and the value an array containing all the occurrences for that day. To transform the object into an array of the same … Read more

Moment.js “Invalid date”

Suppose your date is in format ‘DD-MM-YYYY’ & you want to display it in DD-MMM format then you can do this by first telling moment function which format your date currently is & then asking it to convert it to required format. Below is the example: var taskDueDate = moment(dueDate, ‘DD-MM-YYYY’).format(‘DD-MMM’); This way you can … Read more

Get time difference between datetimes

This approach will work ONLY when the total duration is less than 24 hours: var now = “04/09/2013 15:00:00”; var then = “04/09/2013 14:20:30″; moment.utc(moment(now,”DD/MM/YYYY HH:mm:ss”).diff(moment(then,”DD/MM/YYYY HH:mm:ss”))).format(“HH:mm:ss”) // outputs: “00:39:30” If you have 24 hours or more, the hours will reset to zero with the above approach, so it is not ideal. If you want … Read more