Convert Date for UI5 table, maybe in bindProperty - date

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

Related

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';
}
}

Datepicker noweekend and date range

I try to disabled the date for holidays and weekend but that not work
My code :
$(function() {
$('.thing12DatePicker').datepicker(
{
beforeShowDay: function (date) {
var startDate = "2017-06-17",
endDate = "2017-06-21",
dateRange = [];
for (var d = new Date(startDate); d <= new Date(endDate); d.setDate(d.getDate() + 1)) {
dateRange.push($.datepicker.formatDate('yy-mm-dd', d)&& $.datepicker.noWeekends);
}
var dateString = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [dateRange.indexOf(dateString) == -1];
}
});
});
The date holidays are disabled but not the weekend !
I want disbled the weekend .
thx
I have found solution
var startDate = "2017-06-17",
endDate = "2017-06-21",
bankHoliDays = [];
for (var d = new Date(startDate); d <= new Date(endDate); d.setDate(d.getDate() + 1)){
bankHoliDays.push($.datepicker.formatDate('yy-mm-dd', d));
}
function disableDates(date) {
var dt = $.datepicker.formatDate('yy-mm-dd', date);
var noWeekend = jQuery.datepicker.noWeekends(date);
return noWeekend[0] ? (($.inArray(dt, bankHoliDays) < 0) ? [true] : [false]) : noWeekend;
}
$(function () {
$('#datepicker').datepicker({
beforeShowDay: disableDates
});
});

Deleting a row based on date - Date values

My query relates to this Google Form responses spreadsheet. I'm trying to adapt the script I got from here.
function cleanup() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form responses 1');
var values = sheet.getDataRange().getValues();
var InAYear = (Date.now()/86400000 + 25569) + 365;
for (var i = values.length - 1; i >= 0; i--) {
if ( values[i][5] >= InAYear) {
sheet.deleteRow(i+1);
}
}
}
I'm trying to get this to compare the date in the Start Date column of the sheet with the date in a year from now and delete the row if the column entry is greater than this (ie. if the date on the sheet is more than a year in advance). However, I obviously don't understand how to get the two different dates in the same format because examining variable values when debugging shows wildly different values.
Try the following script code:
function cleanup() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form responses 1');
var values = sheet.getDataRange().getValues();
var today = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), 'MM/dd/yyyy')
for (var i = values.length - 1; i >= 0; i--) {
if ( values[i][4] != '' && dateDiffInDays(values[i][4],today) > 365 ) {
sheet.deleteRow(i+1);
}
}
};
function dateDiffInDays(d1,d2) {
var date1 = new Date(d1);
var date2 = new Date(d2);
var timeDiff = date1.getTime() - date2.getTime();
return Math.ceil(timeDiff / (1000 * 3600 * 24));
};
It looks like I have it working, and sending me an email.
function cleanup(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form responses 1');
var values = sheet.getDataRange().getValues();
var today = new Date();
var InAYear = new Date();
InAYear.setFullYear( today.getFullYear()+1 );
var emailaddress = "****";
var subject = "Annual Leave Request";
var message = "Annual Leave has been requested as follows:" + "\n\n";
for (var i = values.length - 1; i >= 0; i--) {
if ( values[i][4] > InAYear ) {
sheet.deleteRow(i+1);
subject = "Annual Leave Request - Rejected";
message = "The following annual leave request was rejected due to being more than one year in advance:" + "\n\n";
}
}
for(var field in e.namedValues) {
message += field + ':'
+ "\n" + e.namedValues[field].toString() + "\n\n";
}
MailApp.sendEmail(emailaddress, subject, message);
}
Thank you to Kishan, without who's help I would not have been able to get to this stage.

Inserting Date&Time on Google Sheets

On Google Sheets;
I want it to display Day/Month/Year & Hour/Minute
When I click Insert date it shows 6/20/2015 21:59:4
I want it to show 20/06/2015 21:59
I have tried a few things and I managed to change the month/day around but it threw up errors so had to start again.
If anyone has better code or can fix this code It would help alot!
function getDate() {
var d = new Date();
return (d.getMonth()+1) + "/" + d.getDate() + "/" + d.getFullYear();
}
function getTime() {
var d = new Date(),
offset = -d.getTimezoneOffset()/60,
h = d.getUTCHours() + offset,
m = d.getMinutes(),
s = d.getSeconds();
return h + ":" + m + ":" + s;
}
function getDateAndTime() {
return getDate() + " " + getTime();
}
function insertValue(theValue, format) {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
theSheet = ss.getActiveSheet(),
theCell = theSheet.getActiveSelection();
theCell.setNumberFormat(format || theCell.getNumberFormat());
theCell.setValue(theValue);
return true;
}
function getValue() {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getValue();
}
function getFormat() {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getNumberFormat();
}
function insertDate() {
insertValue(getDate(), "Date");
}
function insertTime() {
insertValue(getTime(), "Time");
}
function insertDateAndTime() {
insertValue(getDateAndTime(), "Date time");
}
function appendDate() {
insertValue(getValue() + " " + getDate());
}
function appendTime() {
insertValue(getValue() + " " + getTime());
}
function appendDateAndTime() {
insertValue(getValue() + " " + getDateAndTime());
}
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name: "Insert Date", functionName: "insertDate"});
menuEntries.push({name: "Insert Time", functionName: "insertTime"});
menuEntries.push({name: "Insert Date and Time", functionName: "insertDateAndTime"});
menuEntries.push(null); // line separator
menuEntries.push({name: "Append Date", functionName: "appendDate"});
menuEntries.push({name: "Append Time", functionName: "appendTime"});
menuEntries.push({name: "Append Date and Time", functionName: "appendDateAndTime"});
ss.addMenu("Insert Date", menuEntries);
}
function onInstall() { onOpen(); }
does this formula work as you want:
=TEXT(NOW(),"dd/mm/yyyy hh:mm")
Maybe use Utilities.formatDate().
See here for more info
example:
var formattedDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd/MM/yyyy HH:mm");

validate end date equal to greater than start 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; }
}