Field Date Time Retrieve only the year

Hello,

I would like to be able via a Date Time Field to Retrieve only the year of this field and display it in another field of my view in non-editable. Is it possible on the view side via XML or do I have to go through JAVA code?

Thank you for your answers.

column.getYear() will give you year if the column is date time

or if you need current year date.getYear()

try this and let me know if it is not working

My need is this: I need that when the user enters (onChange) his date in a dateTime field (fromDate).
Another field (yearDate) captures by doing a split for example on the fromDate field and updates the yearDate field.

I don’t know where I can put my script in the frameWork view.

Otherwise I can do it in java but hey just for a split it bothers me to make a method just for that.

I know you can use groovy but I don’t know how I refer to my fields etc…

I need an example I think

you want to validate input?

 I'm looking to do this:

just update my orderYear field

Do you understand ?

if you are saving full date why you need just year?

Hello,

Because the date field already exists, and I need to keep it but just extract the year from it.

The starting DataTime field is this:

image

I’d like to extract the year from it and update this String field here:

image

So 2022 for this exemple.

Yeah, use the onChange event on your field and use an action-method to call a java method which calculate your year separatly and return with :

response.setValue(« orderOfYear », value);

Like here : axelor-open-suite/axelor-helpdesk/src/main/java/com/axelor/apps/helpdesk/web/TicketController.java at 924c3a995c67a527b4f054f7136b1961dd8721e9 · axelor/axelor-open-suite · GitHub

Be sure to add orderOfYear to your domain so it will be save in database and display when you open your view.

Hello again,

I have this little piece of java code suddenly to retrieve my year, I have my String field in my view, and domain.

in domain :

<field name="orderOfYear" readonly="true" />

in view :

<string name="orderOfYear"/>

My Java :
Project project = request.getContext().asType(Project.class);

OrderedService orderedService = Beans.get(OrderedService.class);
String yearNow = orderedService.yearStartOrdered();
project.setOrderOfYear(yearNow);
response.setValue("orderOfYear", project.getOrderOfYear());
response.setReload(true);

On the other hand, I cannot get the view side display of the value returned in response.setValue(« orderOfYear », project.getOrderOfYear());

Did I make a mistake somewhere?

I should see my date directly in my view side string field by doing the setValue()? Or are there other things to do?

in form you can show it like this

<field name="fromDate">
  <viewer><![CDATA[
  <strong>{{$moment(record.fromDate).format('YYYY')}}</strong>
  ]]></viewer>
</field>

Thank you for your answer, it works, indeed it takes it hot view side via angular I saw. Strange, that I failed to display it from JAVA, in my answer the hashMap contains my value yet but good.

Ce sujet a été automatiquement fermé après 30 jours. Aucune réponse n’est permise dorénavant.