Google script not recognising date - date

I'm trying to write some code to check values in a spreadsheet column to see if they are valid dates. However, even if I put in a date in a cell with the format set to date, it doesn't seem to recognise it as a date. When I debug the date objects are listed as "new Date" but if I attempt to ask Logger to getDate() I receive the following error :
TypeError: Cannot find function getDate in object Sat Jun 05 2010 01:00:00 GMT+0100 (BST). (line 15, file "myCode")
I can set my objects as dates by calling 'new Date()' which means that they are recognised as dates but this changes all the objects to dates whether they should be or not.
Here is my code:
function onMyPress(){
var sheet = SpreadsheetApp.getActive().getActiveSheet()
var myRange = sheet.getRange(2,2,8,1)
var myValues = myRange.getValues()
for (i=0; i<8; i++){
var testDate = myValues[i]
if (isDate(testDate)) {sheet.getRange(i+2,3,1,1).setValue("Date")
}
else{
sheet.getRange(i+2,3,1,1).setValue("Not Date")
}
Logger.log(testDate.getDate())
}
}
function isDate(testDate) {
if (Object.prototype.toString.call(testDate) !== "[object Date]") {
return false;
}
else {
return true;
}
}
Thanks for looking.

The problem is that getValues() returns a 2D array and you script assigns a "row" (a
JavaScript array) instead of cell value (a JavaScript string, Date, etc.) to testDate on the following line
var testDate = myValues[i]
One alternative is to replace the above code line by by something like the following:
var testDate = myValues[i][0]
assuming that your dates are on the first column.

"Cannot find function getDate in object" means that you are calling getDate() on an object that is NOT a Date object.
I am guessing "testDate" is a String object? If you step through the code, does it go into the "else" clause?
You need to:
Check that your String object contains a correctly formatted date
(which seems to be the case)
Then CONVERT it to a Date object, like this: var realDate = new Date(testDate);
See working example here:
https://jsfiddle.net/uyowax8o/
// Convert to Date object
var realDate = new Date(testDate);
// This now works
alert(realDate.getDate());

Related

How to format Yup date() to brazilian format?

I'm wanting to use yup to validate an input, that is being used as Birthday. By first I added the simple .date() function to the yup shaped object, but it won't accept Brazilian dates, as the format is same as European (dd/MM/YYYY), how do I proceed?
Use .transform() to parse your value with a function passing the following parameters:
value as current Date parsed by yup.
originalValue as the original inputted value (e.g. "01/01/2022").
yup.date().transform((value, originalValue) => {
// Do your parsing structure
return newValue
})
Remember that your newValue must be a Date object since you want to validate a yup.date() type in your form.
You can use this example as solution for you to parse your value to "dd/MM/YYYY" format:
// Input: '10/12/2022'
// yup.object({ [...]
birthday: yup.date().transform((value, originalValue) => {
// originalValue = "10/12/2022"
try {
let date = originalValue.split('/')
// date = ["10", "12", "2022"] <- day, month and year respectively
if (date.length === 3) {
let newDate = `${date[2]}-${date[1]}-${date[0]}`
return new Date(newDate)
}
return null
} catch (e) {
return null
}
}),
// [...] })
// Expected Output: '2022-10-12T00:00:00.000Z'
See yup docs about parsing values using .transform(): Yup Schema Basics - Parsing: Transforms

Compare two Dates in SAPUI5

I want to compare, whether Date A is greater than Date B. But I always get false, even if Date A is greater.
var oDatepicker = this.getView().byId("Date");
var oFormat = sap.ui.core.format.DateFormat.getInstance({ pattern: "d.M.y" });
var oDate = oFormat.format(new Date());
var oDatepickerParsed = oFormat.parse(oDatepicker.getValue());
if(oFormat.format(oDatepickerParsed) > oDate){
return true;
} else {
return false;
}
I tried to instantiate a Date-Object based on oDatepicker.getValue() to compare Date-Object with Date-Object, but there is something wrong.
var oDateObject = new Date(oDatepicker.getValue())
oDatepicker.getValue() is = '01.11.2020' type string. Whats wrong?
Did you try the DatePicker method getDateValue() which gives you "the date as JavaScript Date object. This is independent from any formatter."

Changing Pentaho date values to first and last day of the week in Javascript Modified Value

I am trying to set two variables to the first and last day of the week for a given date, but the .setDate() method does not seem to be changing the date and 'lastday' and 'firstday' variables return an Invalid Date (1970)
DateNew is from my input step which is defined as dd/MM/yyyy format
var curr = DateNew;
var first = getDayNumber(curr,"d") - getDayNumber(curr,"wm")
var last = first + 7;
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
Declaring the input variable as a date seemed to resolve the issue as there was no javascript type associated with it.
var curr = new Date(DateNew);
var first = getDayNumber(curr,"d") - getDayNumber(curr,"wm")
var last = first + 7;
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();

Compare data with present date google script

Hi i want to compare column with date (i.e "Referral Date" column)
with present day , here is what i have
function newF(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Worksheet');
var range = ss.getDataRange();
var headers = range.getValues()[0];
var colIndex = headers.indexOf("Referral Date");
var today = new Date();
var searchRange = ss.getRange(2,colIndex+1,ss.getLastRow()-1);
for (i=0;i<range.getLastRow();i++){
var dates = searchRange.getValues();
if (today.valueOf()>dates.valueOf()){
updatelFilter()
} else{
SpreadsheetApp.getUi().alert('Future Date Error');
break;
}
}
}
The problem i have is, it throws alert Future Date Error irrespective of date in column (Referral Date). Let me know if additional information is required.
My goal:
1)if date column (Referral Date) is greater than present date : Throw alert error & should not run updateFilter
2)if (Referral Date) is lesser than present date: Run updateFilter function
Issues
searchRange.getValues() yields a two dimensional array. So dates[0][0] points to a date, while dates[0] points to an array.
var dates = searchRange.getValues(); is being called inside the loop repeatedly, when it should ideally be called outside once since the value will not change; calling it inside the loop is costly and redundant
for (i=0;i<range.getLastRow();i++){ the condition can be replaced with i<dates.length if point 2 is followed
if (today.valueOf()>dates.valueOf()){ I believe is supposed to have dates[0] instead
Modified Code
function newF(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Worksheet');
var range = ss.getDataRange();
var headers = range.getValues()[0];
var colIndex = headers.indexOf("Referral Date");
var today = new Date();
var searchRange = ss.getRange(2,colIndex+1,ss.getLastRow()-1);
var dates = searchRange.getValues().map(d=>d[0]);
for (i=0;i<dates.length;i++) {
if (today.valueOf()>dates[i].valueOf()){
updateFilter()
} else {
SpreadsheetApp.getUi().alert('Future Date Error');
break;
}
}
}
To run updateFilter only if no future dates
Replace the loop with the following -
if(dates.some(d => today.valueOf() < d.valueOf())) {
SpreadsheetApp.getUi().alert('Future Date Error');
} else {
for (let i=0; i<dates.length; i++) {
updateFilter();
}
}

Getting all list of items and passing it into an array

I am working on a project and I am trying get a list of items and return it into an array to use in my project.
This is my code:
let identifier = delete.items
var identifiers: [String] = []
identifier.forEach({ (listModel) in
identifiers = ["\(String(describing: listModel.remiderDate))"]
})
print("ITEMS DATES \(String(describing: identifiers.count))")
The identifier returns an array of values not reminderDate alone but I am focused on getting the reminderDate.
Printing the identifies returns 1 whereas the number expected to return 5.
Your loop assigns a new value to identifiers each time so only the last value remains.
You probably want to use map:
let identifiers = delete.items.map { String(describing: $0.reminderDate) }
print("ITEMS DATES \(identifiers.count)")
Please note that using String(describing:) should only be used for debugging. Use a DateFormatter to convert a Date to a String.