Save date as timestamp in CQ5 - aem

I need t use the CQ5 xtype 'datefield' since I need only the date and not time tombe entered by the author.
But the issue is that 'datefield' saves the date in JCR as a String and not as a timestap [ as it does when 'datetime' is used.]
Is there a work around to save the date as a timestamp ?

I don't think it is possible to save the date as timestamp using datefield, without meddling with the default script. But as a workaround, you can use datetime and set the property hideTime to true, to hide the time part, so that the author will not be able to author it.
The json for the config is shown below.
{ "fieldLabel":"Date",
"xtype":"datetime",
"hideTime":true,
"name":"./date",
"defaultValue":"now",
"jcr:primaryType":"cq:Widget"
}
You can add defaultValue to 'now', if you want current date to be initialized as the default, if not explicitly filled in by the author, else it can be ignored.
NOTE: The defaultValue: 'now' doesnt work for me in IE (i am using IE 11 and emulating the previous versions through dev tools), but it works fine in Chrome and Mozilla.

A rough workaround for your jsp:
<%#page import="java.text.SimpleDateFormat,java.util.Date"%>
<%
SimpleDateFormat displayDateFormat = new SimpleDateFormat("dd MMM yyyy");
String dateField = properties.get("nameofdatefield", "");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
Date formattedDate = sdf.parse(dateField);
String formattedDateStr = displayDateFormat.format(formattedDate);
out.println('Example of formated string'+formattedDateStr);
%>
From the above, you can also convert it to a Date Object, depending on what you wish to manipulate.
Let me know if the above example helps

Related

Extract the date format from react-intl

I have a component that uses a datepicker. The datepicker needs a dateFormat property that fits the momentjs pattern, for example 'DD.MM.YYYY' or 'MM/DD/YYYY'.
The date formatting is handled by react-intl. This works fine when converting from a date to a string (via formatDate). However, I need to retrieve the pattern as described above.
My goal is to do something like
dateFormat = this.props.intl.extractDateFormat() // returns 'DD.MM.YYYY'
I have found this similar question, but the only answer relies on parsing the string, which I cannot do, because I do not know whether Day or Month will come first in the formatted date.
If it is possible to convert this string to a date and somehow retrieve the format from momentjs, that would also be a good solution.
I was able to get the date format from react-intl. To do this, I defined an example date and had it formatted by react-intl, and then parsed the format by referring to the original string.
My component which is exported as injectIntl(Component) has this method:
deriveDateFormat = () => {
const isoString = '2018-09-25' // example date!
const intlString = this.formatDate(isoString) // generate a formatted date
const dateParts = isoString.split('-') // prepare to replace with pattern parts
return intlString
.replace(dateParts[2], 'DD')
.replace(dateParts[1], 'MM')
.replace(dateParts[0], 'YYYY')
}
The date will e.g. be formatted to '09/25/2018', and this function would return 'MM/DD/YYYY', a format which can be used by Moment.js.
This function only works if you know that the month and day will always be displayed with two digits. It would fail if the format is something like 9/25/2018.
I have not found a way to extract the date format from react-intl directly.

Add day to date with momentjs

I am trying to add a day to my date:
let createdDate = moment(new Date()).utc().format();
let expirationDate = moment(createdDate).add(1, 'd');
console.log(expirationDate);
However, this keeps returning an obscure object {_i: "2017-12-20T21:06:21+00:00", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array(7), …}
fiddle:
http://jsfiddle.net/rLjQx/4982/
Does anyone know what I might be doing wrong?
You are logging a moment object. As the Internal Properties guide states:
To print out the value of a Moment, use .format(), .toString() or .toISOString().
let createdDate = moment(new Date()).utc().format();
let expirationDate = moment(createdDate).add(1, 'd');
console.log(expirationDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
Please note that you can get the current date using moment() (no need to use new Date()) or moment.utc().
I will go with this one, simple works for me and I don't think you need other function to only add day in moment.
var yourPreviousDate = new Date();
var yourExpectedDate = moment(yourPreviousDate).add(1, 'd')._d;
The add method modifies the moment object. So when you log it, you're not getting an obscure object, you're getting the moment object you're working with. Are you expecting a formatted date? Then use format or some other method.
I agree with other answers just providing shortcut and different ways
You can do the format at the same time
moment().add(1,'d').format('YYYY-MM-DD');
or you can just format any date or date object
moment(result.StartDate).format('YYYY-MM-DD');

'MM-YYYY' date format with tinyDatePicker

Want to display 'MM-YYYYY' Date format with tinyDatePicker. they have timeFormatAttribute attribute to set format but it does not works as expected.
tiny datepick
I am using this following code to format date but its not changing.
new DatePicker('.datepicker-fca', {
timeFormatAttribute:'MM-YYYY'
});
timeFormatAttribute shall be name of attribute. Not format directly ('MM-YYYY' how you using it). See here.
And how it shall be used you can see here.
...
timeFormat: '',
timeFormatAttribute:'data-timeformat',
doAMPM: false,
...

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.

Passing date in Date data type only in groovy

I have been struggling with this even after doing so much of research on such a simple thing, so I need some help here.
I need to pass current date in date data type only in 'yyyy-MM-dd' format. SimpleDateFormat converts current date to string type and while trying to parse though it gets converted to Date type but changes the format.
I need currentDate in the format 'yyyy-MM-dd' of Date Type.
if(!session.dtfromDate && !session.dttoDate)
eq("startDate", currentDate)
I have managed to figure out a solutions to this. First got my desired format which obviously converted it to a String and then parsed this to a Date. It has worked perfectly fine.
SimpleDateFormat nsdf = new SimpleDateFormat('yyyy-MM-dd')
String currentDate = new SimpleDateFormat('yyyy-MM-dd').format(new Date());
Date newDate = (Date)nsdf.parse(currentDate)
if(!session.dtfromDate && !session.dttoDate)
eq("startDate", newDate)