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,
...
Related
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.
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.
let's say I have a Date as a String, formated in yyyy-MM-dd, and I want it to be formated as style:"short".
I want just to use Dateformat.
I used this https://openui5.hana.ondemand.com/#docs/guide/91f2eba36f4d1014b6dd926db0e91070.html to get an idea of how to use DateFormat.
But I can't see, what's wrong with my code:
date: function(sdate) {
var regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
if (!sdate.match(regex))
return "no valid date given";
jQuery.sap.require("sap.ui.core.format.DateFormat");
var oDateFormat = sap.ui.core.format.DateFormat.getInstance({pattern: "yyyy-MM-dd", style: "short"});
return oDateFormat.format(sdate); //date should be returned here in "short"-style
}
The console tell's me
TypeError: j.getTime is not a function.
Also it seems like the WebIDE doesn't know a function Datetime.format().
Can you help?
You'd probably reread the documentation in your link: to convert String into JS Date, you have to use DateFormat.parse method.
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')
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