dates with "and" at the last date freemarker - freemarker

I have a list of dates seperated by comas. but I want an "and" before the last date.
I want it to look like this:
date, date, date and (last)date.
my code:
<#list MySequence as x>${x}<#sep>,</#list>
cant figure how to find the last date and add the "and" to it
thanks

You could decide based on the current index:
<#list MySequence as x>${x}<#sep><#if x?index == MySequence?size - 2> and <#else>, </#if></#list>
It's not very terse... so if you will do this often, create a macro for it.

Related

Split up date range into chunks to join back into one table KDB+/Q

I have a table that is being joined like so
result: select from table where date within (sd;ed)
where sd and ed span multiple months (like sd:2021.07.01 ed:2021.09.30). The table that I'm querying from has a break if you take more than a month, so to get the result I need, I have to do something like the following:
result: uj(uj(select from table where date within (2021.07.01;2021.07.30);select from table where date within (2021.08.01;2021.08.31));select from table where date within (2021.09.01;2021.09.30))
How can I make this dynamic for any sd and ed? That is, how can I break up time range into first days of months, last days of months, and join them all into one table cleanly? My initial idea was to divide the days in the range x amount of time, to be input by a user, then add the number of days that results to the sd to get frames, but that got messy.
Something like this should chunk it for you:
raze{select from t where date within x}each(first;last)#\:/:d group"m"$d:sd+til 1+ed-sd
Do not use where date.month=x as you had suggested - at least not for historical queries
One option for converting your start and end dates into an iterable list of dates might be:
f:{0N 2#(x,raze -1 0+/:`date$mx+1+til(`month$y)-mx:`month$x),y}
Where x is start date and y is end date.
f[2021.07.14;2022.02.09]
2021.07.14 2021.07.31
2021.08.01 2021.08.31
2021.09.01 2021.09.30
2021.10.01 2021.10.31
2021.11.01 2021.11.30
2021.12.01 2021.12.31
2022.01.01 2022.01.31
2022.02.01 2022.02.09
Then you could run:
{select from t where date within x} each f[sd;ed]
And join the results using raze or (uj/)

How to convert dates within an array to a specific format

I have an array of dates, where the dates have different formats. I would like to convert them all to the same format, however what I've tried has caused me some problems.
I get the dates from a SQL query:
table_birth_dates = self.class.connection("SELECT birth_date FROM #{temp_table_name}").values
which gives me an array of dates:
[
[0] "10/3/80",
[1] "10/3/81",
[2] "10/3/01",
[3] "33/33/1970",
]
I want to get the year as a full year like above.
I tried:
table_birth_dates.first.to_time
and got:
ArgumentError: argument out of range
I also tried:
Date.strptime(table_birth_dates)
and got:
no implicit conversion of Array into String
Does anyone have any ideas?
As the others already said: Don't store arbitrary strings as date! Use your database and store dates as date or datetime. See documentation of your database. (See comment from #the-tin-man.)
That said you can try the following with your current date:
table.map! { |date|
date_hash = Date._strptime(date, '%m/%d/%Y') # {:mon=>10, :mday=>3, :year=>70}
if date_hash.nil?
'invalid'
else
date_hash[:year] = date_hash[:year] + 1900 if date_hash[:year] < 1000
Date.new(date_hash[:year], date_hash[:mon], date_hash[:mday])
end
}
This gives you an array with actual dates like so:
[
1980-10-03
1981-10-03
1901-10-03
invalid
]
Of course you should do something about this invalid but it serves your main purpose.
If all you dates is on the the same format as the first 3 dates you can use this con change the format of the dates:
dates.map! do |date|
Date.strptime(date, '%d/%m/%y').strftime('%d/%m/%Y')
end
If you have dates on different formats you could list the formats you have and try to parse each of them and see if you get a valid result, however dates like 33/33/1970 is not a valid date at all.
Parsing dates isn't as straightforward as people assume when they first start working with them, because dates are represented differently around the world.
'10/3/80'
would represent "October 3, 1980" in a U.S. formatted date, but would be "March 10, 1980" in the rest of the world. And, the software has no idea which it is unless you tell it which to use. And, if your data comes from around the world you can't tell unless you know what locale the data was generated in or unless they explicitly tell you what format it's in.
require 'date'
Date.strptime('10/3/80', '%m/%d/%y') # => #<Date: 1980-10-03 ((2444516j,0s,0n),+0s,2299161j)>
Date.strptime('10/3/80', '%d/%m/%y') # => #<Date: 1980-03-10 ((2444309j,0s,0n),+0s,2299161j)>
From DB, bring date in integer format and convert back to any specific format.
Are you storing date in DB as string and trying to convert them for unique format to display? If so, you can correct while storing the value (or) add a column in DB which gets updated whenever your date column gets inserted/updated and retrieve from new column.

Retrieve database records between two weekdays

I have several records in my database, the table has a column named "weekday" where I store a weekday like "mon" or "fri". Now from the frontend when a user does search the parameters posted to the server are startday and endDay.
Now I would like to retrieve all records between startDay and endDay. We can assume startDay is "mon" and endDay is "sun". I do not currently know how to do this.
Create another table with the names of the days and their corresponding number. Then you'd just need to join up your current table with the days table by name, and then use the numbers in that table to do your queries.
Not exactly practical, but it is possible to convert sun,mon,tue to numbers using MySQL.
Setup a static year and week number like 201610 for the 10th week of this year, then use a combination of DATE_FORMAT with STR_TO_DATE:
DATE_FORMAT(STR_TO_DATE('201610 mon', '%X%V %a'), '%w')
DATE_FORMAT(STR_TO_DATE('201610 sun', '%X%V %a'), '%w')
DATE_FORMAT(STR_TO_DATE('201610 tue', '%X%V %a'), '%w')
These 3 statements will evaluate to 0,1,2 respectively.
The main thing this is doing is converting the %a format (Sun-Sat) to the %w format (0-6)
well i don't know the architecture of your application as i think storing and querying a week day string is not appropriate, but i can tell you a work around this.
make a helper function which return you an array of weekdays in the range i-e
function getWeekDaysArray($startWeekDay, $endWeekDay) {
returns $daysArray['mon','tue','wed'];
}
$daysRangeArray = getWeekDaysArray('mon', 'wed');
now with this array you can query in table
DB::table('TableName')->whereIn('week_day', $daysRangeArray)->get();
Hope this help

Filter Date for Reporting

I am currently working with reports in Navision by a "book sales" currently use 2 variables "Date" one for the start and one for the end of the filter, but I wonder if you can only use a single variable where the user directly enter "month" and "year". Thank You!
If you are setting the filter with SETRANGE you need two values. But if you use SETFILTER you can use a string with a date filter like this:
010115..103115
p1..p3
and the just:
DateField.SETFILTER(filterstring);
Yes, when the user put the month and year data you by code transform this info in date values example:
Variables from and to = date
from := DMY2DATE(1, Month, Year);
EVALUATE(to, FORMAT(CALCDATE('+1M', Desde) -1));
YourTable.SETRANGE("Posting Date", from, to);

How to show date in normal format in jqgrid when formatter is used

Date column is created using colmodel below.
This column shows values like 0101.0101.7070 for every date column.
if formatter:date is removed, date is correct.
How to show normal date values with formatter:date ?
{ "name":"Date",
"formatter":"date",
"datefmt":"dd.mm.yyyy",
"formatoptions":{"newformat":"dd.mm.yyyy"},
"editable":true
}
Update.
Data is passed from server using json in same dd.mm.yyyy format like
{"total":2,"page":1,"records":57,"rows":
[{"id":"9279","cell":["9279","42","","10.08.2011","","","","False"]},
{"id":"9278","cell":["9278","41","","12.08.2011","","","","False"]},
...
Using d.m.y formats in column options as suggested shows proper dates but with 2 year digits only..
I'm looking for a 4-digit year numbers. I tried d.m.yyyy format but this shows 8 digit year numbers and 1 for month and day as 01.01.70707070
I also tried to add srcformat: 'dd.mm.yyyy' to formatoptions but this does not change the wrong result.
To display the day as the number you should use j or d. For the displaying month as the number you should use n or m. The d and m includes 0 padding at the beginning if needed. The 'y' means two digit year format and Y means four digit year.
So you probably need srcformat: 'd.m.Y' or srcformat: 'j.n.Y'.
Use d.m.y instead of dd.mm.yy ,

Resources