Reputation: 635
I have a code that runs to select a date from calender, but I want to set the current_date
to that of the server in the mentioned calender. And with a functionality that compares client_date
with server_date
.
Upvotes: 3
Views: 2351
Reputation: 2709
One way of doing it is to pass the current date from your server script to the template, so when the pages renders, javascript can access it and make comparison. This has obvious issue - if the user will leave the page open till next day, the date will be out of date.
Other way is to use asynchronous call to your back-end and obtain the date from there.
Upvotes: 0
Reputation: 328674
That's possible. As suggested elsewhere, you should convert the dates to strings for two reasons:
To send the current date to the client, you can use a hidden form field or an AJAX request or a web service or REST or JSON or JSONP or ... well, there are too many methods to do it.
To compare dates, convert the string to a Date
object and then use the usual methods to compare them (compareTo()
in Java and <
,==
,>
in JavaScript).
Upvotes: 2
Reputation: 3948
The easiest way is to compare both dates with conversation to milliseconds. Both languages provides methods for it.
Javascript:
<script type="">
var myDate = new Date();
var milliseconds = myDate.getTime();
</script>
Java (JSP):
<script type="text/javascript">
<%!
Date myJavaDate = new Date();
long myJavaMilliseconds = 0L;
myJavaMilliseconds = myJavaDate.getTime();
%>
var myJavaMillisondsInJs = <%= myJavaMilliseconds %>;
</script>
/* Put code here to compare between those dates */
If you want to compare the dateas server side you have to submit the javatime to the server via Ajax or a form.
Upvotes: 0
Reputation: 7035
You can do it by using the following way:
In Java
public class YourJavaClass{
public static String getServerDate()
{
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);
}
}
In jsp
$('input.datepicker').datepicker(
{
minDate : new Date(<%out.print(YourJavaClass.getServerDate());%>)
});
Upvotes: 1