0

For a simple leave application in UI5, i need to increment or decrement the date of a new sap.ui.commons.DatePicker by one day on each button press, how can I get and set the new date?

The problem is that the DatePicker returns only a string of the Date. I tried already new Date(datePicker.getYyyymmdd()) but then I cat an Invalide date back.

Matthias
  • 461
  • 7
  • 24

2 Answers2

0

A possible solution for your parsing the string to a date object can be found here or here.

You should also consider to switch to sap.m.DatePicker since sap.ui.commons.DatePicker is deprecated in version >=1.38 (don't know which version you are using). The sap.m.DatePicker also has the method getDateValue, which should already return a date object.

Community
  • 1
  • 1
Rene
  • 331
  • 7
  • 16
  • I changed now to sap.m.DatePicker. Somehow in the Documentation it isn't marked as deprecated. – Matthias Aug 17 '16 at 09:32
  • Actually the whole sap.ui.commons library is deprecated and yes, it is marked: https://sapui5.hana.ondemand.com/sdk/#docs/api/symbols/sap.ui.commons.DatePicker.html – cschuff Aug 17 '16 at 13:04
0

Date manipulation in Javascript is really easy. If you want to add a day to a date, it would be date.setDate(date.getDate() + 1), and subtracting works in the same fashion: date.setDate(date.getDate() - 1).

You just have to get the yyyymmdd format as used by the date-picker back into Javascript's native date type. As mentioned by @R.Schmitt, you could of course use any of Javascript tricks from Stackoverflow. On the other hand, the UI5 team has already built this logic for you in the DateFormat class, so it's probably neatest to leverage that.

To get from a yyyymmdd to date, you could use:

sap.ui.core.format.DateFormat.getDateInstance({pattern : "YYYYMMdd" }).parse(date);

To get from a date to yyyymmdd format, you can use:

sap.ui.core.format.DateFormat.getDateInstance({pattern : "YYYYMMdd"}).format(yyymmdd);

To see the Javascript date calcluation and UI5's DateFormat class in action, have a look at this jsbin: http://jsbin.com/xeyiwic/1/edit?html,output

jpenninkhof
  • 1,920
  • 1
  • 11
  • 12
  • I think date manipulation in JavaScript really sucks. If you have used moment.js once you'd never want to go back again :) – cschuff Aug 17 '16 at 13:04
  • If you come from a Java world, you'll know what a blessing the native Dates in Javascript already are. I agree with you that moment.js makes that experience even better. – jpenninkhof Aug 18 '16 at 11:17