Nette how to subtract timestamp from current date - date

how can i subtract number of days (timestamp|date'z') from current day (date('z')) in latte? I've tryes to use var but that does not like the formating (z).

Latte filters, unlike function calls, are not something that can be applied to any part of an expression – they are only optional feature of the variable printing macro.
{expression_to_be_printed|filter1|filter2|filter3}
date filter mostly just calls format method so you can use it directly:
{(new DateTime())->format('z') - $timestamp->format('z')}
This, however, will not work if the $timestamp lies in a different year.
To fix this, you can use DateTime’s diff method. DateInterval, returned by the method, can then be formatted using format method which provides difference in the number of days through %a formatting string.
{$timestamp->diff(new DateTime())->format('%a')}
Fortunately, the date filter also allows formatting intervals.
{$timestamp->diff(new DateTime())|date:'%a'}
Admittedly, this looks kind of ugly. A better way would be to define a custom filter so you could just use {$post->timestamp|daysAgo}. See Latte docs about creating your own filters.

Related

Swift String into formula

This may be the wrong place to ask this but I am aware of NSExpression, but it seems it can only do primitive mathematics - am I wrong about this? I am able to do something like this: #Value-2/3+9-6 ect easily, but I would like to incorporate Max's, Min's, and possible a few other operations (instead of just multiplication, division, subtraction, and addition. Is that possible in the same equation, does it have to be converted multiple times? Any advice would be appreciated!
You could try to use a "function" expression but that requires a rather unwieldy syntax in the expression string and is probably a stretch of the intended purpose of NSExpression unless you are actually implementing an aggregate type MIN/MAX function for predicates on a dataset.
I figured it out.
For anyone who needs this - you can take any string and use:
functionName(x) for most of the functions like sqrt, multiplyby, trunc, ceiling, ect
And then for the six with multiple variables (max, min, count, average, sum, ect) you use functionName({x,y}).
All can be used in a string with expressionValue(with: nil, context: nil)

Difference between Formatter and Factory Function

​Hello,
Please explain what is the difference between Factory and Formatter function. Because as I see both can be used to format or manipulate the output results. How to choose between both of them ?
Regards,
Mayank
Factory functions allows you to create different types of controls in runtime. Let's assume that you have a list and you want to display different type of list items according to your list index for instance, or maybe to some value that you have in your model. Factory functions allows you to do it in the binding way.
Formatters are some kind of an helper functions which receive and input and return an output. The most popular examples are date and time that you receive date in form A and return date in form B. formatter functions are defined on a property level so if you have a field in your list item which display a date you can use formatter in order to do a very simple manipulation to this date

Converting a UNIX timestamp into a Date object in Elm

As the title reads, how can I convert a UNIX timestamp, e.g. 1458297862, into a Date object in Elm?
Date.fromString does not seem to accept it, and Date.fromTime gives the wrong answer.
You can use Date.fromTime, but you have to multiply that value by 1000.
This gives you the date you'd expect:
Date.fromTime 1458297862000
I came across this answer and it didn't quite work in 0.18.
I found that Date.fromTime wouldn't accept arbitrary integers. The reason is that it's expecting floats, so you'd really want:
Date.fromTime (toFloat 1458297862000)

simple math issue

I'm trying to modify an existing JasperReports template for an invoice. I would like to do some simple math in one field, i.e. calcultate the tax from an existing gross value. The gross value is represented by a parameter $P{prevAdvanceLine1Sum} and in the next field I would like to simply divide this value by a number (in my case 1.23).
Can you give me the expression I should use and what expression class to set?
You can either use simple arithmetic operators (like " / " in this case. See example here: EL Basic Arithmetic), or use the pre-defined methods provided by iReports. Which means, right-click upon the TextField and select Edit Expression. Then select the divide() method.
So the two methods would be:
1. $P{prevAdvanceLine1Sum / 1.23}
2. $P{prevAdvanceLine1Sum}.divide( 1.23 )

Using a parameter value as a CDate() function input

I have a parameter {?Calendar Year} that takes on year values (2014,2015, etc.) that I would like to use in a formula. I would like it to function how one might think this would work:
{table.date}>CDate({?Calendar Year},01,01).
Does anyone know how I can accomplish this?