XPages convert a date in a view column that is displayed as HTML - date

In a view panel, I have one col that is displayed as HTML because I am pointing to documents created with a traditional form. So, I can't set the "Display type" to anything except a String. I've tried to convert the value by using toJavaDate, but that didn't work.
Here is the formula for my HTML column where rowData is the var or view panel. myserver/folder/myapp.nsf part I changed so that I could paste it here...
if (!rowData.isCategory())
var disp = rowData.getColumnValue("PayPeriod");
"<a href='https://myserver/folder/myapp.nsf/0/"+rowData.getUniversalID()+"?OpenDocument'>"
+disp+"</a>"
The link works properly in my view, but the value displayed shows a full date & time value (6/15/13 8:51 AM). All I am trying to do is display it as 06/15/2013 (MM/DD/YYYY)

if the column is set to date you could use this to get the correct date format
if (!rowData.isCategory())
var formatter
if(viewScope.formatter){
formatter=viewScope.formatter
}else{
formatter = new java.text.SimpleDateFormat( "MM/dd/yyyy" );
viewScope.formatter=formatter
}
var d = rowData.getColumnValue("PayPeriod");
var disp=formatter.format(d)
"<a href='https://myserver/folder/myapp.nsf/0/"+rowData.getUniversalID()+"?OpenDocument'>"
+disp+"</a>"

Related

Conditional formatting for dates

I'm trying to come up with a simple conditional format formula for highlighting cells that have a date that is greater than three months older than today's date. It seems though that the "Date is before" option only gives a few options, none of them seem to allow what I'm looking for. Is there a custom formula that could accomplish this?
Edit: attaching a snip of the column in question:
Formula :
=DAYS(now(),B2)>90
Go to the custom formula in the conditional formating rules and use this:
=DATEDIF(A1,TODAY(),"D")<90
try:
=1*C2>DATE(YEAR(TODAY()), MONTH(TODAY())+3, DAY(TODAY()))
also make sure you have valid dates and not plain text dates. you can test this with ISDATE formula
You can use Apps Script and a Custom Menu in order to solve your issue with setting the color in the cell depending on the date. Go to Tools->Script Editor and paste this code:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Check Difference', 'dateDifference')
.addToUi();
}
function dateDifference(){
var sheet = SpreadsheetApp.getActiveSheet().getActiveRange(); // Get the selected range on the sheet
var dates = sheet.getValues(); // Get the values in the selected range
var oneDay = 1000*60*60*24;
var row = 1;
var re = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/; // This will help you to check if it's really a date
dates.forEach(function(el){ // Iterate over each value
if(typeof el[0] == 'object'){ // Check if it's really a date
var gmtZone = el[0].toString().split(" ")[4].split(":")[0]; // Take the date's GMT
var dateFormatted = Utilities.formatDate(el[0], gmtZone, "dd/MM/yyyy"); // Format the date
if(re.test(dateFormatted)){ // Test if it's the right format
// This part will calculate the difference between the current date and the future date
var futureDateMs = new Date(el[0]);
var todayDateMs = (new Date()).getTime();
var differenceInMs = futureDateMs - todayDateMs;
var differenceInDays = Math.round(differenceInMs/oneDay);
if(differenceInDays >= 91.2501){ // Test if the difference it's greater to 91.2501 days (3 motnhs)
sheet.getCell(row, 1).setBackground("#00FF00"); // Set the color to the cell
}
row++;
}
}
});
Save it by clicking on File->Save.
Then you can select a range in a column and click on Custom Menu->Check Difference as you can see in the next image:
As you can see, you will get the desired result:
Notice
It's really important to be careful with what you consider to be a "month", I mean how many days you are going to take into consideration. In my code, I took Google's suggestion of 1 = 30.4167.
Docs
These are other Docs I read to be able to help you:
Utilities.formatDate()
Working with Dates and Times.
I hope this approach can help you.

Display the correct dateformat in dateItem dynamicform smartgwt

I'm taking a record fron a grid to populate some item in a dynamicform. The problem is i cannot display dates in the right format for the form, it keeps displaying then as they are in the grid and whatever i do to format the date is just ignored by SmartGWT. I'm a bit confused. Below you'll find my code. Can anyone help me out?
dataTrade = new DateItem();
dataTrade.setTitle(Nove.getInstance().getConstants().dataTrade());
dataTrade.setName(RecordEditMovTitUploadDS.DATA_TRADE);
dataTrade.setWidth(100);
dataTrade.setAlign(Alignment.LEFT);
dataTrade.setUseTextField(true);
String dataT = movTitRecord.getAttribute(ListMovTitByValCodTitDetailDS.DATA_TRADE);
DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date dateTr = null;
try{
dateTr = dateTimeFormat.parse(dataT);
} catch(IllegalArgumentException e){
SC.say("Couldn't parse date");
}
DateTimeFormat dateTimeFormat2 = DateTimeFormat.getFormat("dd/MM/yyyy");
String dateTra= dateTimeFormat2.format(dateTr);
//dataTrade.setDisplayFormat(DateDisplayFormat.TOEUROPEANSHORTDATE);
//dataTrade.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
dataTrade.setInputFormat("dd/MM/yyyy");
dataTrade.setDefaultValue(dateTra);
To be more specific in debug the date is correctly formatted, when i pass the value to setDefaultValue it is dd/MM/yyyy as it should but when i load the page the format is still yyyy-MM-dd HH:mm:ss.SSS and i cannot understand why...
Uncommenting this line should work:
dataTrade.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
After adding this line, the date was displaying in the correct format. Also I was able to edit the form and input dates in the format dd/MM/yyyy.

Prefill current date in Google Form with Google Apps Script

I have a Google Form to a spreadsheet and I need a date field pre-filled with the current date.
Something like an "onOpen trigger", which updates the date field or a date field with now().
Is this possible in Google Apps Script?
In your case, some of the points of using the Form remain unclear. Suppose that you have the ability to edit a link to the Form or your users agree to add the bookmarklet to their browser.
Common code
let id='1FAIpQLScx-1H1moCzBfkTEOZnVgBScbJeHZ4YE5E6IY2mNZvMuVcOXA';
window.open(
`https://docs.google.com/forms/d/e/${id}/viewform?usp=pp_url&entry.1000000=`+
(new Date()).toISOString().split('T')[0]
);
Bookmarklet
Just add the next string to the bookmarks bar
javascript:let id = '1FAIpQLScx-1H1moCzBfkTEOZnVgBScbJeHZ4YE5E6IY2mNZvMuVcOXA'; window.open(`https://docs.google.com/forms/d/e/${id}/viewform?usp=pp_url&entry.1000000=` + (new Date()).toISOString().split('T')[0]);
I put a static HTML page on S3 with a few line of Javascript for redirecting. See https://jsfiddle.net/barryku/3etd8qf0/
var formId = "1FAIpQLSc_acGSzp2VeJIyG0tNJwqcNdUPOweLROGenY0Fe56I635VGQ";
var dateField = "entry.1608430301";
var formsUrl = "https://docs.google.com/forms/d/e/"+ formId + "/viewform?usp=pp_url&" + dateField + "=$$today&entry.747437339=Yes";
var today = new Date();
var redirectUrl = formsUrl.replace("$$today", new Date(today.getTime() - (today.getTimezoneOffset() * 60000)).toISOString().split('T')[0]);
window.location.replace(redirectUrl);

How to return a normal javascript date object?

Is it possible to return a javascript date object from this? I have an text input field with the calendar and want to return a standard date object from it's value... Is this possible?
Is this what you are looking for?
var date = '2016-06-12'; // Can be document.getElementById('myField').value that points to your date field.
var date_parts = date.split('-');
var date_obj = new Date(date_parts[0],date_parts[1],date_parts[2]);
console.log(date_obj);
You can also simply use
new Date(document.getElementById('myField').value)
and see if it works. The date function is smart enough to parse based on browser's locale. This should work for time as well. Eg. new Date('2016-06-12 04:15:30')

How to refresh page on select of date from datepicker Liferay

I am using liferay-ui datepicker.
When my page loads the date is current date in the datepicker and data of the current date is loaded on the page.
What I want to do is allow the user to view data such that when user selects any other date, data of the selected date is dispayed.
How should I go about this?
SHould I refresh the entire page? how?
Or should I use ajax? how should i go about? if ajax is to be used how should i pass the data?
EDIT:
I will explain my problem in detail. I am using liferay:ui:date . I want user to select a date from it. Once the user selects date, I want to pass the date to custom-sql. I am calling the finder function in the same jsp as follows:
List<Object[]> ObjdisplayAttListName = AttendanceLocalServiceUtil.findAttendance(currentdate);
I want to pass the user selected date in the above function.
Right now what I am doing is only passing the current date in the above line of code. Iwant to pass the user selected date.
I think you should go with AJAX. Make ajax call when you select date using date-picker
and get that date in your serveResource method. Based on your selected date fetch data and pass that data to your presentation layer in required format i.e JSON.
Collect data and display it. Thats it :)
Let me know if you have problem !!
Personally, I prefer working with liferay-ui:input-date. Just make sure to keep a Date or Calendar object in your Controller Class
<portlet:actionURL var="setDate" name="setDate" >
<portlet:param name="jspPage" value="/html/yourPage.jsp" />
</portlet:actionURL>
<aui:form action="<%= setDate%>" method="post" enctype="multipart/form-data" >
<%
Date date = (Date)renderRequest.getAttribute("_a_date");// Get your Date from the controller
Calendar cal = CalendarFactoryUtil.getCalendar();
cal.setTime(new Date()); // create with current date if this form is presented for the 1st time
if(Validator.isNotNull(date)){
cal.setTime(date); // else use the Date you want to display
}
%>
<liferay-ui:input-date
yearRangeStart="1970"
yearRangeEnd="2100"
formName="pickedDate"
dayParam="dd"
monthParam="mm"
yearParam="yy"
dayValue="<%= cal.get(Calendar.DATE) %>"
monthValue="<%= cal.get(Calendar.MONTH) %>"
yearValue="<%= cal.get(Calendar.YEAR) %>"
/>
<aui:button name="setDateBtn" value="Submit that date" type="submit"/>
</aui:form>
Back to the controller..
public void setDate(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
UploadPortletRequest queryRequest = PortalUtil.getUploadPortletRequest(actionRequest);
int dd = ParamUtil.getInteger(queryRequest, "dd");
int mm = ParamUtil.getInteger(queryRequest, "mm");
int yy = ParamUtil.getInteger(queryRequest, "yy");
String date_format = "yyyy/MM/dd";
SimpleDateFormat sdf = new SimpleDateFormat(date_format);
GregorianCalendar gc = new GregorianCalendar(yy, mm, dd);
Date date = gc.getTime(); // Keep this Date and reload the page sending this Date in an actionRequest param
actionRequest.setAttribute("_a_date", date );