JSTL date comparison/finding date difference - date

I have a problem where I want to compare two dates or in other words I want to find the difference of two dates in JSP using JSTL.
I have seen many solutions like creating a JSP:bean of Date object. Other ones were more messy solutions.
Can anybody please provide me a cleaner solution so that I can do this with JSTL only? I need a solution somewhat like SQL DATEDIFF function in JSTL
Thanks in advance.

Just use <fmt:formatDate> to extract the year from it so that you can do the math on it.
Edit : Ihen use two varibles to store both dates
<fmt:formatDate value="${date1}" pattern="yyyy" var="Date1" />
<fmt:formatDate value="${date2}" pattern="yyyy" var="Date2" />
<c:if test="${Date1 le Date2}">
<doSomeLogic/>
</c:if>

Related

Add week in smarty tpl

Output: 2015-01-20 03:52:19
Need 01.20.2015 + 1 week = 01.27.2015
I curently have {$order[orders].invoice_date|date_format:"%d.%m.%Y"}
But how to add week + 1?
So, I need to add 1 week and then formating this.
But the date isn't in the timestap format.
Smarty version: 3.1.
I can use only smarty logic, not PHP.
How to achieve this?
You don't need a plugin to work this out. It can be solved with a combination of cat and date_format.
Since date_format is a wrapper to PHPs strftime(), you can use the conversion specifiers available in strftime() - and that is what I used tackle the issue at hand.
Try this:
{$order[orders].invoice_date|cat:' +1 week'|date_format:"%d.%m.%Y"}
I've used Smarty version 3.1.17 to recreate your problem. The solution is based on the assumption that the value in your variable $order[orders].invoice_date is a string 2015-01-20 03:52:19.
You can create a smarty plugin, something like this one to suit your needs. http://smarty.incutio.com/?page=AlternativeDateModifierPlugin
You shouldn't be doing this logic in smarty at all. This type of thing should be done in the php code - assign two date values to two separate smarty variables (one with the 1 week added), and use the appropriate one in the appropriate place in your template (applying the appropriate date_format as required).
edit: I know you've said you want to do this using smarty syntax - I'm just pointing out that trying to do this type of manipulation in smarty is not what the template language is designed for. If you only have access to the smarty .tpl files, you might try using the {php} tag to put your php logic in the .tpl file.

JQuery Mobile/Datebox, difference between display date format and submit date format

Is there a way to display a date in an input different from the format I want to submit.
For example I have to submit a date in "yyyy-mm-dd" format but I want to display the date in another format : "dd/mm/yyyy" (french display).
Is there a good tip to do that with Datebox for jQuery Mobile (an option I didn't see ?)
I think I have to cheat in creating an input hidden with the good form format and another one with the format to display (and not submitted), but maybe a better solution exists.
Any ideas ?
Your best bet is to indeed use 2 inputs - but, it's pretty easy to do, and using a callback on the set event, you can even make datebox do the second format for you.
http://dev.jtsage.com/jQM-DateBox2/demos/script/split.html
(Note: I just added the demo, so you didn't miss it earlier)
Just a small addition
It should be "overrideDateFormat":"%d/%m/%Y" in the HTML inline options.
In http://dev.jtsage.com/jQM-DateBox2/demos/script/split.html it states "dateFormatOverride":"%d/%m/%Y"} however this is incorrect and doesn't work. Just a heads up for anyone else with this issue.
Yes you need to use this method.
<!-- fix american date formatting -->
<script type="text/javascript">
jQuery.extend(jQuery.mobile.datebox.prototype.options, {
'overrideDateFormat': '%d/%m/%Y',
'overrideHeaderFormat': '%d/%m/%Y'
});
</script>

Get date difference in jstl

I have a date and want to show difference of it with current time as --year--month--days--hours--minutes--seconds. How can I do this jstl? It is sure that the date will be greater than current datetime.
Using JSTL you could do some code gymnastics like:
<jsp:useBean id="now" class="java.util.Date" />
<fmt:parseNumber
value="${(now.time - otherDate.time) / (1000*60*60*24) }"
integerOnly="true" /> day(s) passed between given dates.
But as code suggests, this gives overall difference and hardly could be a "calendar aware" way of doing it. I.e. You could not say: "3 years, 1 month and 2 days passed since otherDate".
Another example for this "days passed..." style, using a JSP tag and using "today/yesterday/days back" presentation:
<%--[...]--%>
<%#attribute name="otherDate" required="true" type="java.util.Date"%>
<jsp:useBean id="now" class="java.util.Date" scope="request"/>
<fmt:parseNumber
value="${ now.time / (1000*60*60*24) }"
integerOnly="true" var="nowDays" scope="request"/>
<fmt:parseNumber
value="${ otherDate.time / (1000*60*60*24) }"
integerOnly="true" var="otherDays" scope="page"/>
<c:set value="${nowDays - otherDays}" var="dateDiff"/>
<c:choose>
<c:when test="${dateDiff eq 0}">today</c:when>
<c:when test="${dateDiff eq 1}">yesterday</c:when>
<c:otherwise>${dateDiff} day(s) ago</c:otherwise>
<%--[...]--%>
Note:
In your software problem domain, if it makes sense to talk about days and months in a calendar way, probably you should have that expressed in your Domain Model. If not, at least you should benefit from using another lower software layer to provide this information (and for example using java.util.Calendar or Joda-Time APIs).
Not sure there are any built in ways of doing this with JSTL. you could write your own tag library or potentially use expression language (EL) like below.
${(dateObj) - (now.time)}
taken from Looking for JSTL Taglib calculate seconds between two dates
Mark I'm uncertain if this is possible using JSTL and one way would be to create your own custom tag to handle this as #olly_uk suggested. Me personally would not use any expression language on my JSP as this might also affect readability and not best practise.
You could also have this calculation/date difference when your page bean is constructed, that way avoiding any EL or a new tag. This also might have its limitations such as I'm not sure if the date you want to check the difference is entered by the user on field where you want to display the result instantly etc if you see what I mean.
Also another you could try depending on your scenario is using jQuery to calculate and display the difference, I thought I'll link this page anyway from SO.
How do I get the number of days between two dates in JavaScript?
JQuery Calculate Day Difference in 2 date textboxes
Hope this helps.

Convert date from yyyy-mm-dd to dd-mm-yy using xsl

HI all,
I got a JavaScript function to convert the date, but I was wondering if there is any way to convert the date with xsl without using JavaScript.
Thanks.
and the string manipulation, http://www.xml.com/pub/a/2002/05/01/xslt-string.html
Thanks to all of you for answering my question, after looking on those links you left me to take a look I came out with something very simple.
<xsl:for-each select="//MaternityDiaryEvent/DateOfVisit">
<xsl:value-of select="concat(substring(.,9,2),'/',substring(.,6,2),'/',substring(.,3,2))"/>
</xsl:for-each>
Thanks again to all, I'll give you a vote to each of you.
Cesar.
The format-date function might do the job.
You can use the Microsoft formatter.
Or the format-date function, if you are using XSLT 2.0 (which .Net does not).
Found a very good post on date formatting here.
No Javascript required.
Only XSL elements and XPath String functions can do the job.

Ajax Control Toolkit Date Picker - Is it possible to not have to select the day?

So I can set the date format on the Calendar Extender to it displays just the month, but you would still have to select the Year, then the Month, then the Day.
I would like to just select the Year, then the Month.
<cc2:CalendarExtender ID="DateOfReleaseCalendarExtender" runat="server"
TargetControlID="DateOfReleaseTextBox"
Format="MMMM yyyy" />
That is not a built-in feature, no. You'll have to extend it yourself. I would probably hook into the Javascript events--whichever one you think makes sense in your situation.
This requires patching the CalendarExtender part of the AjaxControlToolkit assembly.
I found a blog post that explains how to do something very similar to what you are trying to do: Patching the CalendarExtender Control