I have written some code to check two dates, a start date and an end date. If the end date is before the start date, it will give a prompt that says the end date is before start date.
I also want to add a check for if the start date is before today (today as in the day of which the user uses the application) How would I do this? ( Date checker code below, also all this is written for android if that has any bearing)
if (startYear > endYear) { fill = fill + 1; message = message + "End Date is Before Start Date" + "\n"; } else if (startMonth > endMonth && startYear >= endYear) { fill = fill + 1; message = message + "End Date is Before Start Date" + "\n"; } else if (startDay > endDay && startMonth >= endMonth && startYear >= endYear) { fill = fill + 1; message = message + "End Date is Before Start Date" + "\n"; }
To check if a date is today's date:Use the toDateString() method to compare the two dates. If the method returns 2 equal strings, the date is today's date.
Once you have created your DateTime objects, you can also call the diff() method on one object and pass it the other date in order to calculate the difference between the dates. This will give you back a DateInterval object. $last = new DateTime( "25 Dec 2020" ); $now = new DateTime( "now" );
Using the Date. One way to check if a string is date string with JavaScript is to use the Date. parse method. Date. parse returns a timestamp in milliseconds if the string is a valid date.
Don't complicate it that much. Use this easy way. Import DateUtils java class and call the following methods which returns a boolean.
DateUtils.isSameDay(date1,date2); DateUtils.isSameDay(calender1,calender2); DateUtils.isToday(date1);
For more info refer this article DateUtils Java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With