validate end date equal to greater than start date - date

I want to check whether end date is greater than or equal to start date, with jquery validate. It is validating end date greater than start date, but it is not permitting end date equal to start date,
here is my code -
jQuery.validator.addMethod("greaterThan",
function(value, element, params) {
if (!/Invalid|NaN/.test(new Date(value))) {
return new Date(value) > new Date($(params).val());
}
return isNaN(value) && isNaN($(params).val())
|| (Number(value) > Number($(params).val()));
},'Must be greater than {0}.');
jQuery("#collect_and_delivery").validate({
errorElement:'div',
rules: {
from:{
required:true
},
to:{
required:true
},
start_date:{
required:true
},
end_date:{
required:true,
greaterThan: "#start_date"
}
},
messages: {
from:"Please enter collect address",
to:"Please enter delivery address",
start_date:"Please enter shipping collect date",
end_date:
{
required:"Please enter shipping delivery date",
greaterThan:"Delivery date and Collect date should be proper"
}
}
});

You should use is greater than or equal to(>=) in your custom method greaterThan.
Change this line:
return new Date(value) > new Date($(params).val());
To:
return new Date(value) >= new Date($(params).val());
Enjoy...

If you want to use simple javascript to compare ethe date. Here is one; simple and easier.
function compareDate() {
var str = document.getElementById("start_date").value;
var end = document.getElementById("end_date").value;
var year = str.substring(0,4);
var month = str.substring(5,7);
var date = str.substring(8,10);
var endYear = end.substring(0,4);
var endMonth = end.substring(5,7);
var endDate = end.substring(8,10);
var startDate = new Date(year, month-1, date);
var endDate = new Date(endYear, endMonth-1, endDate);
if (startDate > endDate) {
alert('start date should be less than end date');
return false;
}
else { return true; }
}

Related

Flutter filter list by dates

So I am getting a Promo List from an API that has a dateStart and dateEnd field which returns YYYY-MM-DD String or null if it is empty. Now I want to compare/filter.
1- has dateStart=YYYY-MM-DD && has dateEnd=YYYY-MM-DD - is ok to render but if dateEnd == today or over then it wont show
2- has dateStart=YYYY-MM-DD && has dateEnd=null - is ok to render in the list as it may be a lifetime promo
3- only show in list if dateStart is today or has already passed
Lets assume this is your list:
List sampleList = [
{"dateStart": "2022-11-18", "dateEnd": "2022-11-22"},
{"dateStart": "2022-11-20", "dateEnd": "2022-11-20"},
{"dateStart": "2022-10-20", "dateEnd": "2022-11-19"},
{"dateStart": "2022-10-20", "dateEnd": null},
];
so you can get your list like this:
var result = sampleList.map((e) {
if (e['dateStart'] != null) {
DateTime startDate =
DateFormat("yyyy-MM-dd").parse(e['dateStart'] + ' 23:59');
DateTime? endDate = e['dateEnd'] != null
? DateFormat("yyyy-MM-dd hh:mm").parse(e['dateEnd'] + ' 23:59')
: null;
var now = DateTime.now();
if ((startDate.isBefore(now) || startDate == now) &&
(endDate == null || endDate.isBefore(now))) {
return e;
}
}
}).toList();
result.removeWhere((element) => element == null);
print("result = $result");//result = [{dateStart: 2022-10-20, dateEnd: 2022-11-19}, {dateStart: 2022-10-20, dateEnd: null}]
now you can use result to show your list.

Calculate the difference of dates in days

I need to calculate the difference in days between two dates in two differente occasions
Occasion 1 - There's a start and an end date
Occasion 2 - There's a start date and a "IN PROGRESS" where the end date should be
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var daTa = ss.getSheetByName("Data");
var daysColumn = daTa.getRange('C2:C' + daTa.getLastRow()).getValues();
var startDate = daTa.getRange('C2:C'+ daTa.getLastRow()).getValues().flat();
var endDate = daTa.getRange('A2:A'+ daTa.getLastRow()).getValues().flat();
var today = new Date().valueOf();
endDate.forEach((finaldate,row) => {
if(finaldate == "IN PROGRESS") {
daysColumn[row][0] = (parseInt(startDate,10)-today);
} else {
daysColumn[row][0] = (parseInt(startDate,10)-parseInt(finaldate,10));
}})
daTa.getRange(2,4,daysColumn.length, 1).setValues(daysColumn)
}
Right now i got this bit of code, and it know what needs to be done, but it returns only "#NUM!" values on the column, where it should print the numbers.
Probably you want this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var daTa = ss.getSheetByName("Data");
var daysColumn = daTa.getRange('C2:C' + daTa.getLastRow()).getValues();
var startDate = daTa.getRange('C2:C' + daTa.getLastRow()).getValues().flat()
.map(x => new Date(x).valueOf()); // get milliseconds of the dates
var endDate = daTa.getRange('A2:A' + daTa.getLastRow()).getValues().flat()
.map(x => new Date(x).valueOf()); // get milliseconds of the dates
var today = new Date().valueOf();
var day = 1000 * 60 * 60 * 24; // milliseconds in a day
endDate.forEach((finaldate, row) => {
if (finaldate == "IN PROGRESS") {
daysColumn[row][0] = (startDate[row] - today) / day;
} else {
daysColumn[row][0] = (startDate[row] - finaldate) / day;
}
})
daTa.getRange(2, 4, daysColumn.length, 1).setValues(daysColumn)
}
function DiffInDays(Day1,Day2) {
if(Day1 && Day2 && (Object.prototype.toString.call(Day1) === '[object Date]') && (Object.prototype.toString.call(Day2) === '[object Date]')) {
var day=86400000;
var t1=new Date(Day1).valueOf();
var t2=new Date(Day2).valueOf();
var d=Math.abs(t2-t1);
var days=Math.floor(d/day);
//Logger.log(days);
return days;
} else {
throw 'Invalid Inputs';
}
}

How to return a "date" from a Google Sheet, and use it in another function?

I am trying to find a date in a sheet and it does work, but when I return the startdate it does not function correctly.
function startdayfinder(NomClient)
{
const passageclientsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Client Sheet");
const numlastrowIS = passageclientsheet.getLastRow()-1;
const dataIS = passageclientsheet.getRange(2,1,numlastrowIS,3).getValues();
var start;
dataIS.forEach(function(row,i)
{
if (row[0]==NomClient)
{
if(row[2]=="oui")
{
start = Utilities.formatDate(row[1], "GMT+1", "dd/MM/yyyy");
}
else
{
Logger.log(start);
return start;//(Utilities.formatDate(start, "GMT+1", "dd/MM/yyyy"));
}
}
});
}
And this is how I call this function:
var startday = startdayfinder(row[0]) ;
Unfortunately, I don't know where it goes wrong because the function returns null, but the logger shows that the date has been successfully written into the start variable.
Where did I mess up?
Try with getDisplayValues()
const dataIS = passageclientsheet.getRange(2,1,numlastrowIS,3).getDisplayValues()
assuming that dates are also formatted as dd/MM/yyyy in your sheet

Convert Date for UI5 table, maybe in bindProperty

I have a table with 2 columns: Keys and Values.
The keys are dates, formatted with the sap time format.
Something like this:
"/Date(1510700400000)/"
I have a function which shows the date in a readable form.
convertDate: function() {
if (value === 'null') {
return value.replace('null', 'no date specified')
} else {
var d = new Date(parseInt(value.replace('/Date(', '').replace(')/', ''), 10))
}
var month = d.getUTCMonth() + 1 // months from 1-12
var day = d.getUTCDate()
var year = d.getUTCFullYear()
return year + '/' + month + '/' + day
},
I access my value via:
oTable2.addColumn(new sap.ui.table.Column({
label: new sap.ui.commons.Label({
text: "RDATE"
}),
sortProperty: "RDATE",
template: new sap.ui.commons.TextView().bindProperty("text", "key")
}));
Now I want to call my function on "key".
How can I accomplish that? :)
You can achieve this using a formatter function.
template: new sap.ui.commons.TextView().bindProperty("text", {
path: "key",
formatter: function(value){
if (value === 'null') {
return value.replace('null', 'no date specified')
} else {
var d = new Date(parseInt(value.replace('/Date(', '').replace(')/', ''), 10))
}
var month = d.getUTCMonth() + 1 // months from 1-12
var day = d.getUTCDate()
var year = d.getUTCFullYear()
return year + '/' + month + '/' + day
}
});
Reference: https://sapui5.netweaver.ondemand.com/#/topic/07e4b920f5734fd78fdaa236f26236d8

moment js issue with french format

ACTUAL ISSUE:
am using momentjs and below code to validate date format
date = moment(dateVal, fmt);
if format is YYYY-MM-DD, and if dateVal is 2017-04, then date value is not coming as null. moment function is sending 2017-04-01 as the date back instead of "invalid date" or null. how do i change the code for the moment to show invalid date.
i tried this
date = moment(dateVal, fmt, true);
but for some reason when i use this, isValid is always coming as false, even when i send valid date and format. for eg, 1/4/2017 with format MM/DD/YYYY is giving error with date = moment(dateVal, fmt, true)
here is my code
var CustomDateValidate = function (dateVal, fmt) {
var date = null;
if (dateVal !== null && dateVal !== undefined) {
fmt = defaultFor(fmt, getHiddenData().dateFormatJavascriptMoment); // here for en the fmt will be "MM/DD/YYYY" and for fr the fmt will be "YYYY-MM-DD"
date = moment(dateVal, fmt, true);
if (!(date.isValid())) {
date = null;
}
//else
if (date.year().toString().length < 4) {
date = null;
} else if (date.toString().toLowerCase() === 'invalid date') {
date = null;
} else if (date._i.toString().length > 10) {
date = null;
}
}
return date;
plz help.