How to get max date from any date field in moment js - momentjs

How to extract max date from date field by using any function in moment js?

I guess this can help
var a = moment().subtract(1, 'day');
var b = moment().add(1, 'day');
moment.min(a, b); // where a, b are moment objects
output: a
moment.max(a, b);
output: b

Related

How to use for loop to create multiple dates?

I have a function here that gets the date, and adds one week to it:
func thingy() {
let currentDate = Date()
var dateComponent = DateComponents()
dateComponent.day = 7
let futureDate = Calendar.current.date(byAdding: (dateComponent*i), to: currentDate)
print(futureDate!.formatted())
}
This gets the current date, adds one week to it, and prints out that date.
I want to get a for loop that will give the date, for example maybe 10 weeks in the future, maybe looking something like this:
for i in 1...num[ex: 11] {
let currentDate = Date()
var dateComponent = DateComponents()
dateComponent.day = 7
let futureDate = Calendar.current.date(byAdding: (dateComponent*i), to: currentDate)
let match = (title: "Test", date: futureDate)
}
I get this error:
Referencing operator function '*' on 'DurationProtocol' requires that 'DateComponents' conform to 'DurationProtocol'
How do I fix this?
I would advise adding .weekOfYear to the date. E.g., to get an array of Date representing the next ten weeks:
let calendar = Calendar.current
let today = calendar.startOfDay(for: Date())
let dates = (1 ... 10)
.compactMap { calendar.date(byAdding: .weekOfYear, value: $0, to: today) }

I'm using moment.js diff to compare two times, but the result is incorrect

I'm using moment.js diff to compare two times,but the result is incorrect
var moment = require('moment.js')
console.show()
var mydate = new Date();
myhours = mydate.getHours()
myminutes = mydate.getMinutes()
var a = moment([2007, 0, 29]);
var b = moment([2007, parseInt(myhours), parseInt(myminutes)]);
log(b.diff(a,'minutes'))
I want the result to be correct
You are trying to use the hours and minutes of myDate like month and day in the array to pass at moment constructor.
You can change b with this code:
const b = moment([2007, 0, 29, parseInt(myhours), parseInt(myminutes)]);
or using the methods of moment:
const b = moment().year(2007).startOf('minute');
.year() set the year of date, and .startOf() rounds the time to start of minute.
This can be a solution, if I understood what do you want:
const moment = require('moment.js');
const a = moment([2007, 0, 29]); // 2007-01-29 00:00:00
const b = moment().year(2007).startOf('minute'); // 2007-01-12 14:34:00
console.log(b.diff(a, 'minutes')); // -23606 -> use Math.abs(b.diff(a, 'minutes')) for the absolute value, your choice.

Using getTime in a For loop in google sheets script

I am trying to run a 'for' loop that looks like this:
function runMultipleDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//9/26/2018 is 1569477600000 and 2/28/2019 is 1551337200000; 9/21/2018 is 1537509600000
for (var date1 = 1537596000000 /*9/21/2018*/; date1 < 1569477600000 /*9/26/2018*/; date1 +=86400000) {
//runEverything();
var date2 = new Date();
date2 = date2.setTime(date1);
ss.getSheetByName('Time Range').getRange("A3").setValue(date2);
};
};
My goal is to run a function called "runEverything()" that reference a date located in cell A3 of a sheet called 'Time Range'. As long as the date in cell A3 is less that 9/26/2018, the for loop should run my function 'runEverything' and then set a new date in A3. When I do my test loop, the setValue(date2) returns a numeric value rather than a date in that cell (A3). Can someone point out how I can return the date format rather than the numeric value of date? if there is a more elegant way to achieve this, I am all ears!
Thank you for your insight!
You want to use the date string instead of the unix time
If my understanding is correct, how about the following script? Please think of this as just one of several possible answers.
Sample script:
function runMultipleDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var start = new Date("2018-09-29"); // <--- 1538200800000
var end = new Date("2018-10-05"); // <--- 1538719200000
var step = 1; // 1 day (86400000)
for (var date1 = start; date1 <= end; date1.setDate(date1.getDate() + step)) {
runEverything();
var date2 = new Date(date1.getTime());
date2.setDate(date2.getDate() + 1);
ss.getSheetByName('Time Range').getRange("A3").setValue(date2);
};
}
This script brings the same result with the script of your answer.
When the script is run, 10/06/2018 is put to the cell "A3".
References:
getDate()
setDate()
Tanaike suggested using new Date(date2) in the setValue() and it worked. My final script is something like this:
function runMultipleDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
for (var date1 = 1538200800000 ; date1 <= 1538719200000; date1 +=86400000) {
runEverything();
var date2 = new Date();
date2 = date2.setTime(date1+86400000);
ss.getSheetByName('Time Range').getRange("A3").setValue(new Date(date2));
};

Javascript Moment.js add a day to a date in milliseconds

I am trying to add a day to a date in milliseconds.
The code I am using is bellow.
var x = 1450612800000;
var timeFrame = 'days'
x = moment(x).add(timeFrame, 1);
console.log(x['_i']) //returns 1450612800000
Here is the fix:
var x = 1450612800000;
var timeFrame = 'd'
var newDay = moment(x).add(tf, 1);
console.log('newDay');
//Get New Date in Milliseconds format
console.log(newDay.valueOf());
//Get New Date in Date Format
console.log(newDay.toDate());
Take a look at this StackOverflow answer: https://stackoverflow.com/a/28132227/3692354
_i isn't what you want to use here - that's the input that was used to create the moment object. I think what you want to use instead is moment's valueOf function: http://momentjs.com/docs/#/displaying/unix-offset/

How to refresh displayed date in CalendarExtender?

I've this code that changes the maximum date of a second Calendar Extender to 90 days after the date that has been defined in a first one and minimum to the same it has been selected on the first and it works right except for one thing.
var cal2 = $find("calendar2");
var fecha = cal._selectedDate;
var date = fecha.getDate() + 90;
var year = fecha.getFullYear();
var month = fecha.getMonth();
var todayDate = new Date(year, month, date);
cal2._startDate = cal._selectedDate;
cal2._selectedDate = fecha;
cal2._switchMonth(fecha);
cal2._endDate = todayDate;
Problem is that if I first seelct a date on cal, dates are properly shown in cal2, but I select one on cal again then cal2 doesn't display in the same month that cal, what's much worse it displays to select days that would be now impossible to select and in fact you can select them unless you go back first to month mode.
Any idea on how to "refresh" the behavior of the second CalendarExtender?
Thank you.
you can use this method.
Sys.Extended.UI.CalendarBehavior.prototype.refresh = function () {
this._isOpen = true;
this._ensureCalendar();
this.invalidate();
this._isOpen = false;
}
When you add the method only call:
cal2.refresh()

Resources