Fluid compare dates - typo3

I want to compare two dates. I want to make a condition which checks if the date in my database is older than the date in two days.
Here are my two variables:
{<f:format.date format="d.m.Y">+2 days</f:format.date>}
{<f:format.date format="d.m.Y">{day.valid}</f:format.date>}
I want to solve this in the template, not in the model or the controller.

Assign your date to variable »yourdate«.
<f:if condition="{f:format.date(date: '+2 days', format: 'Y-m-d')} < {f:format.date(date: yourdate, format: 'Y-m-d')}">
<f:then>
yourdate is smaller than now + 2 days.
</f:then>
<f:else>
yourdate is greater than or equal to now + 2 days.
</f:else>
</f:if>

Here is my current solution which adds in a current date and does some calculations with the date from the content.
In the controller, add the current date to the data:
$this->view->assign('date_now', new \DateTime());
This is available as {date_now} in fluid then:
<f:if condition="{f:format.date(date: date_now, format: '(Y-m-d)')} > {f:format.date(date: '{event.date}-4 weeks', format: '(Y-m-d)')}">
<f:then>
<p>Event date is past</p>
</f:then>
<f:else>
<p>Event date is upcoming</p>
</f:else>
</f:if>
Note how on the right side, where some calculation is done, additional quotes and curly brackets come in ('{event.date}-4 weeks').
PS I prefer the Y-m-d format to U for a date comparison, as we don't want to compare the current time – just the date.

Convert the date to an unix timestamp using format="U" and compare them. You need to add a variable which contains the comparing date.
<f:if condition="{f:format.date(date: day.valid, format: 'U')} > {f:format.date(date: date_compare, format: 'U')}">
Date is valid
</f:if>

Actually: because DateTime has a getTimestamp method (http://php.net/manual/en/datetime.gettimestamp.php, since 5.3.0) which perfectly conforms to the supported getter method naming in Fluid, the following is perfectly possible. Given $date1 and $date2 are both DateTime instances assigned to the template:
<f:if condition="{date1.timestamp} < {date2.timestamp}">...</f:if>
Will compare the two dates as unix timestamp integers without the need to format the dates. So for your case, assign a $date = (new \DateTime('now'))->modify('+2 days'); from your controller action and compare to that in Fluid. Or just assign the time() timestamp and compare directly to that, skipping the DateTime usage.

Related

TYPO3, Fluid template. Display a div if the date of today is between 05/01 and 10/01

I want to display a div if the date of today is between "05/01" and "10/01". Could someone please help me with it? :)
I could only display it when the date is between "01/01" and "10/01" by writing this code:
<f:if condition="{f:format.date(date: '10/01' format: 'm/d')} < {f:format.date(date: 'now', format: 'm/d')}">
<f:then><div>The Store is closed.</div></f:then>
<f:else><div>The Store is open!</div></f:else>
</f:if>
Many thanks in advance!
Combine two conditions by "&&"
<f:if condition="{f:format.date(date: 'today', format: 'Y-m-d')} >=
{f:format.date(date: '2019/02/27', format: 'Y-m-d')} && {f:format.date(date:
'today', format: 'Y-m-d')} <= {f:format.date(date: '2019/03/01', format: 'Y-m-d')}">
<f:then>
<div>The Store is closed.</div>
</f:then>
<f:else>
<div>The Store is open!</div>
</f:else>
</f:if>
Condition 1 (date of today equal or larger Feb 27):
{f:format.date(date: 'today', format: 'Y-m-d')} >= {f:format.date(date: '2019/02/27', format: 'Y-m-d')}
Condition 2 (date of today less or equal March 01):
{f:format.date(date:'today', format: 'Y-m-d')} <= {f:format.date(date: '2019/03/01', format: 'Y-m-d')}

Watir,click on a date from a date picker 62 days from current date

I need to click on a date that is 62 days later than the current date.
The next button for the months is clickable so I have been able to move the calendar to 2 months later but unable to select the date.
Eg if the current date is 4 July then I need to select 6th Sept.
My code at the moment looks somewhat like this.
b.element(:name, "policyperiod_endDate").click <-- datetime picker's click
b.element(:xpath, "(//button[#type='button'])[50]").click <-- next month
b.element(:xpath, "(//button[#type='button'])[50]").click
b.element(:link,"06").click <--doesnt select 08
b.element(:xpath, "(//button[#type='button'])[68]").click <-selects fixed cell n
how to select 06?
The outer HTML is like this:
input type="text" class="pull-left pdr35 ng-isolate-scope ng-pristine ng- invalid ng-invalid-required" datepicker-popup="yyyy-MM-dd" is-open="datePickerOpts.datepickers.policyperiod_startdate[1]" ng-model="policyIVOs.policyEndDate" name="**policyperiod_endDate**" datepicker-options="dateOptions" show-button-bar="false" close-on-date-selection="true" show-weeks="false" ng-click="open($event, 'policyperiod_startdate', 1)" min-date="policyEndMin" max-date="policyEndMax" required="" readonly=""

Bootstrap 3 DateTimePicker (eonasdan) - How to quickly select year? (for birthdays)

Is it possible in eonasdan's bootstrap 3 datetimepicker to change the dayViewHeaderFormat to show decades or a drop-down menu to choose a year quickly?
Because when someone wants to insert their birthday, it might take a lot of clicks to select the correct year and date. Is there an option to quickly select the correct year? If so, how would this work?
Yes, this is possible using the viewMode function. From the documentation:
viewMode(newViewMode)
Takes a string. Valid values are 'days', 'months', 'years' and 'decades'
For example:
$('#birthday_years').datetimepicker({
format: 'Do of MMMM, YYYY',
defaultDate: new Date(1990, 0, 1, 00, 01),
viewMode: 'years',
});
Here, I've initialised the defaultDate to 1990. You can, of course, change this or use a placeholder to display a text in the input field. Like so: <input ... ... placeholder="Pick your birthday" />.
I've also formatted the date. You can change the date format with these options.
Demonstration of the final result:
> GO TO DEMO

ColdFusion createdate()

I am just learning about ColdFusion function CreateDate. But, when I use CreateDate the value output is different. I mean it changes month to day and day to month.
<cfoutput>
<cfset txtBirthDate='07-10-1983'>
<cfset valueOf_txtBirthDate = dateFormat(CreateDate(Year(txtBirthDate),Month(txtBirthDate),Day(txtBirthDate)),'YYYY-MMM-DD')>
#txtBirthDate#<br/><br/>
#valueOf_txtBirthDate#<br/>
</cfoutput>
The value of txtBirthDate is 07-10-1983, but the value of valueOf_txtBirthDate that CreateDate created is 1983-Jul-10. Why is it July? It is supposed to be October: 07(date), 10(month), 1983(year).
Is there something wrong with the format?
You are using createDate() in a very convoluted way. txtBirthDate isn't a date, so you shouldn't use it as an input for date functions like year(), month() etc. The input should already be a date object by the time you use a date function.
Let's say you start with the string 07-10-1983, where the format is mm-dd-yyyy.
txtBirthDate ='07-10-1983'; // dd-mm-yyyy
// extract the specific date parts from the string
yyyy = listLast(txtBirthDate, "-");
mm = listGetAt(txtBirthDate, 2, "-");
dd = listFirst(txtBirthDate, "-");
// create a date object out of those components
birthDate = createDate(yyyy, mm, dd);
// output the date object in human readable format
writeOutput(dateFormat(birthDate, "YYYY-MM-DD"));
(obviously in real code you'd not have those comments which serve only to state the obvious!)
Only ever use dateFormat() at the last point as you output it. For other date operations, use the actual date object: birthDate.
Because by default your date is mm/dd/yyyy format. So
<cfset txtBirthDate='07-10-1983'>
will be read as July 10th, 1983 by CF. Hence the outout...
You can use Java function to perform this.
This is how it works:
<cfset txtBirthDate='07-10-1983' />
<cfset formatter = createObject("java","java.text.SimpleDateFormat") />
<cfset formatter.init("dd-MM-yyyy") />
<cfset birthDate= formatter.parse(txtBirthDate) />
it seems like should do the trick:
<cfset txtBirthDate = '10-7-1983'>
#DateFormat(txtBirthDate, "MMM, D, YYYY")#
notice i reversed the position of month and day in cfset to make it come out right.
you can do something simple like that :
txtBirthDate = '07-10-1983'; //dd-mm-yyyy
arrayDate = listToArray(txtBirthDate, '-');
date_birthDate = createDate(arrayDate[3], arrayDate[2], arrayDate[1]);
writeOutput(lsDateFormat(date_birthDate, 'yyyy-mm-dd');
<cfscript>
txtBirthDate=CreateODBCDate("1983-10-07");
txtBirthDate=LSDateFormat(txtBirthDate, "dd/mm/yyyy");
</cfscript>
<cfoutput>
#txtBirthDate#
</cfoutput>

How to convert relative dates like "Today" and "Yesterday" to an XQuery date object?

XQuery has a set of useful functions for date conversion. But how to convert relative dates like "Today" and "Yesterday" into the actual date?
For example "Today, 17:33" should be converted to "2012-05-30", and "Yesterday, 22:13" to "2012-05-29".
1. Parse the Date string
Parse the date string. I provided a small function which splits the word indicating the date off and parses it. I added some more convenient names for dates, but you can easily add more if neccessary, notice I convert to lower-case! It uses XQuery date and time functions for calculating the matching date.
declare function local:from-relative-date($string as xs:string) as xs:date {
switch (lower-case(substring-before($string, ",")))
case "today" return current-date()
case "yesterday" return current-date() - xs:dayTimeDuration('P1D')
case "day before yesterday" return current-date() - 2 * xs:dayTimeDuration('P1D')
case "tomorrow" return current-date() + xs:dayTimeDuration('P1D')
case "day after tomorrow" return current-date() + 2 * xs:dayTimeDuration('P1D')
default return error(xs:QName("XPTY0004"), "Unknown Date")
};
2. Format the date
Now use the XQuery 3.0 function format-date(...) (I hope your XQuery engine supports it, BaseX which I used for this example does) to format the date string like you need it:
format-date(local:from-relative-date("Yesterday, 22:13"), "[Y]-[M00]-[D00]")
If it doesn't, you will have to use year-from-date(...) and according functions for month and day, use some number formatting and concatenate yourself.