I have date variable in freemarker. i want to check that date is today or yesterday. If that date is today then need to display Today otherwise display Yesterday. Please can any one help me?
If you are sure that these are only possibilities (today and yesterday), then you could define a function like:
<#function isToday(aDate)>
<#return .now?date?iso_local == aDate?date?iso_local>
</#function>
and then you can use it for example as
${isToday(theDate)?string("today", "yesterday")}
Note that you will need FreeMarker 2.3.17 or later for that function to work. Also be careful with the Server time-zone, as that's what it uses in this example (unless you set that in the FreeMarker configuration manually... or with #setting in the templates).
${houradd(date, 1)?string("yyyy-MM-dd HH:mm:ss")}
${houradd(date, -1)?string("yyyy-MM-dd HH:mm:ss")}
Related
Im trying to build a email template with Freemarker/Clickdimensions plugin in CRM 2013. I have a "Date only" field on an entity which for example contains the date 2017-04-17. I want this date to show as the following: Monday 17 april.
This is done with Freemarker and I have tried the following:
<#assign x = Recipient.field_booking.field_scheduleddate?time>
${x?string.full}
This doesnt seem to work. Im not getting any result at all, just an empty line.
Does anyone know what could be wrong?
I will assume that field_scheduleddate is a string (not a java.util.Date).
At ?time FreeMarker should throw and exception saying something like that the string doesn't follow the expected pattern. I suspect the framework you are using catches and suppresses that exception (which makes using FreeMarker much much harder). Check the logs, maybe it's there.
You want to deal with a date-only value there, hence you should use ?date, as ?time is for time-only values. Also, field_scheduleddate apparently uses ISO 8601 format, so unless the date_format configuration setting is set to ISO, you will have to use ?date.iso (supported since FreeMarker 2.3.21).
As of printing the date, ?string.full should work, but usually you should set date_format globally to the format you prefer, and then you can simply write ${x}.
(Also note that #assign is unnecessary above, as you can put arbitrarily complex expression inside ${}.)
I am new to Ftl and am working with dates in ftl. I know that I can get the current year using
${.now?string("yyyy")}
However I want to assign the year into a variable. On using
<#assign year = ${.now?string("yyyy")}>
I get a syntax error. Can someone help?
I get this syntax error, and unless you are using an old version, you should too:
Syntax error in template "adhoc.ftl" in line 1, column 17: You can't
use "${" here as you are already in FreeMarker-expression-mode. Thus,
instead of ${myExpression}, just write myExpression. (${...} is only
needed where otherwise static text is expected, i.e, outside
FreeMarker tags and ${...}-s.)
The correct assignment is
<#assign year = .now?string("yyyy") />
right now, I have a date that is displayed like this:
19\06\2013
what I want is this :
19\6\2013
my current smarty code is :
{$variable|date_format:"%e\%m\%Y"}
I tried using %n for the month (I assumed that anything that works on php will work in smarty) but it did not give me any result.
How can I display the month without a leading zero in Smarty? is there any workaround solutions? I do not want to edit and format the date in my php file.
Today I have the same problem and this works for me…
Windows server
This solution seems to work only on windows based servers (localhost in my case).
{$variable|date_format:"%d\%#m\%Y"}
Just put hash # before letter 'm'.
I'm using Smarty v3
Linux server:
I moved my project to remote server (probably linux) and hash sign stop to work. I found another solution, just put hyphen - before letter 'm':
{$variable|date_format:"%d\%-m\%Y"}
And this works on linux, but not on windows.
Cross-platform PHP-based solution:
But because smarty is a PHP plugin we can use all of PHP functions. So this is another solution I found, maybe not so elegant but it seems to work everywhere.
{assign var="dateTime" value=$variable|strtotime}
{*
or shorter:
{$dateTime=$variable|strtotime}
*}
<p>{"j/n/Y"|date:$dateTime}</p>
Note:
The above solutions work for formatting day with out leading zero also:
{$variable|date_format:"%-d vs %d vs %#d"}
{$variable|date_format:"j. n. Y":"":"any string - not either strftime or auto"}
then you can write date format as in php function date().
smarty_modifier_date_format($string, $format=null, $default_date='', $formatter='auto')
I have a column called Start Time (it's a SharePoint Default Calendar Column). I need to validate if the Start Time is less than today or not? Without using javascript? Is this possible?
I have tried this:
Created a column called Today type as Date and Time.
Default value is current date.
Then compared the Start Time and Today in validation settings like the following:
=[Start Time] < [Today]
it seems not working. help please?
Try this code instead of yours
=[Start Time]<NOW()
I use this to validate that Start Date must be less than or equal to Today:
=[Start Date]<=Today()
...and it works for me.
I have a report in my application and this report will show a long date from db and I used this expression to make it shorter:
=FormatDateTime(Fields!StatementDate.Value,DateFormat.ShortDate)
and the date will show like this : 1/1/2010
I need to make it like this : 2010/1/1
How I can do it?
That expression do the trick
=CDate(Fields!Fecha.Value).ToString("yyyy/M/d")
I think that it is a lot cleaner to use the Format property rather than format it in your expressions:
http://msdn.microsoft.com/en-us/library/ms252080%28v=vs.90%29.aspx
You can use the standard .NET formatting strings.
Value=Fields!StatementDate.Value
Format=yyyy/M/d
Fields!StatementDate.Value will need to be a DateTime, if not you can try convert it:
Value=CDate(Fields!StatementDate.Value)
=CDate(Fields!StatementDate.Value).ToShortDateString()