on 10-10-2014 7:03 AM
Hi,
How to compare Date in SAPUI5?
solutions apprciated....
Thanks
Hi Deepak,
Try the below approach:
1.Use Javascript substr to get the date,month,year
2.Compare the dates of two strings
Assuming "date" contains your date. You can get the date/month/year as following.
var year = date.substr(0, 4),
month = date.substr(4, 2) - 1,
date = date.substr(6, 2);
If you need this check, in several palces, then create global common function and pass your dates as input to that function.
Regards,
Meganadhan S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
This option should work if you remember to convert the strings (year, month and date) to numbers before doing the comparison. Otherwise "10" < "9". You can do string to number conversion with e.g. parseInt(year, 10). 10 is the radix and it is needed because otherwise e.g. month "08" would be converted in base 8 and would result in integer 0.
This also has the added problem that if the dates happen to be the same day, you also have to compare hours, minutes, seconds and milliseconds in case you need to compare same days also. In that case you can use:
date1.getTime() > date2.getTime()
See this jsbin:
Regards,
Kimmo
Complete guide below---
Controller code:
onCRDChange:function(){ //validates CRD datePicker to ensure user has entered a date on or after today's date
var oCRD= this.byId("crd"),
sToday= new Date(), //get today's year,month,date, eg 2020 Dec 23
sDay= sToday.getDate(), //get just the date, eg 23
sMonth= sToday.getMonth()+1, //get month, but remember its zero-indexed, so Jan would be 0, Dec would be 11. +1 to correct it visually back to 12
sYear= sToday.getFullYear(); //2020
if (sMonth<10){sMonth= '0'+sMonth} //if month is 9, change to 09
if (sDay<10){sMonth= '0'+sDay}
var sToday= sYear+'-'+sMonth+'-'+sDay; //2020-12-23
var sInput= this.byId("crd").getValue(); //get whatever the user inputted. Make sure their input is formatted in yyyy-MM-dd format
if (sInput<sToday){ //yyyy-MM-dd vs. yyyy-MM-dd works as a way to compare dates
oCRD.setValueState("Error");
oCRD.setValueStateText("Please enter today's date or later");
} else {
oCRD.setValueState("None");
}
}
XML View code:
<DatePicker id="crd" change="onCRDChange" placeholder="YYYY-MM-DD" required="true" value="{value:'', type:'sap.ui.model.type.DateTime', formatOptions:{pattern:'yyyy-MM-dd'} }" />
*everything in 'pattern' is case-sensitive so be mindful of that potential error. 'Pattern' ensures the format of the selected date becomes that pattern (instead of, say, 2020Dec23). If user types the date instead of using the pop-up calendar selection, then system would validate to ensure correct format is used via the controller code.
To ensure system returns an error message for auto inline XML validation, make sure you have this line of code in the "sap.ui5" section of your Manifest.json:
"handleValidation":true
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.