formatTime with “h” instead of “:” as separator - fullcalendar

In France, we use the following 24 hour format for time: 18h30.
Therefore, I want to add the "h" char in the timeFormat option of FullCalendar 2.
I found an older post here:
formatTime with "h" instead of ":" as separator
but the solution is apparently not working anymore with FullCalendar v2.
Is there a way I could "escape" the "h" character in the timeFormat option ?

Fullcalendar uses Moment.js for date/times.
Escaping Characters:
To escape characters in format strings, you can wrap the characters in
square brackets.
moment().format('[today] dddd'); // 'today Sunday'
This works for the formatTime fullcalendar option too:
timeFormat: 'H[h](mm)'
Demo JSFiddle

You cannot escape it, however you can use the eventRender method to do the trick for you.
When the event is being rendered parse the DOM and replace the occurrence of : with h.
timeFormat: "H:mm",
eventRender: function(event, element) {
element.find('.fc-time').text(function () {
return $(this).text().replace(":", "h");
});
}

Related

How to assert on a number which is comma separated in cypress

While writing tests on an application I have came across a problem.
I need to fetch a number inside a span from the DOM and then assert if the number is between a specific range.
I can do it by using
cy.get('#my_selector').invoke('text').should('be.gt',lower_bound).and('be.lt',upper_bound)
But the issue is the number is comma separated like 5,000. and I'm getting an error as "expected '5,000' to be a number or a date"
Is there any simple short way to convert it into pure numeric
You can use the javascript replace method to remove the comma and then add a + to convert it into a number, like:
cy.get('#my_selector')
.invoke('text')
.then((num) => {
cy.wrap(+num.replace(/,/g, ''))
.should('be.gt', lower_bound)
.and('be.lt', upper_bound)
})
You can use string.replaceAll() to remove the "," then parseInt()
cy.get('#my_selector')
.invoke('text')
.then(str => parseInt(str.replaceAll(',', '')))
.should('be.within', lower_bound, upper_bound)

Remove unwanted text from string

I have a string "yada yada.useful text here. googletag.cmd.push(function() { googletag.display('div-gpt-ad-447281037690072557-2'); });useful text here. yada yada". I want to remove the string "googletag.cmd.push(function() { googletag.display('div-gpt-ad-447281037690072557-2'); });" but I can't.
I tried selecting the unwanted string using "^(google)});", "^google});" to no avail. Even "^google" or "^google*" does not do anything but "google" works fine. I used the gsub and str_remove functions but my selector doesnt work.
How do I remove the unwanted string? I searched the regex and adding ^ to a selector stops my code from working. What did I miss?
This should do it.
library(stringr)
x <- "yada yada.useful text here. googletag.cmd.push(function() { googletag.display('div-gpt-ad-447281037690072557-2'); });useful text here. yada yada"
x %>% str_remove("googletag.*\\}\\)")
Explanation
The regex looks for "googletag" (where your unwanted string starts)
.* means any number of characters
\\}\\) until we find })
the double backslashes are "R slang" other regex would mostly only use one backslash.

How to format a variable that comes from handlebar?

I'm receiving this "$100.00 - $209.00" that comes from this handlebar (see 2nd line of code)
(that's a price range)
I need to format it, so it only shows the lowest price.
Unfortunately, I cannot format the original code and I need to format the code on HTML or something similiar.
My question is: Is there any way to format it after I receive it?
Thank you
<span class="price after_special>
{{price<?php echo $priceKey; ?>_formated}}
</span>
That code returns $100.00 - $209.00
I need to have just $100
Thanks.
You can format the string using javascript's String.match() function. In below code regex will match:
^ - beginning of string
\$ - $ sign (needs to be escaped)
[0-9]* - numbers from 0-9, * matches 0 or more numbers
The returned item is an array, so you need to get the first value using [0].
let price = '$100.00 - $209.00';
const regex = /^\$[0-9]*/;
let min = price.match(regex)[0];
console.log(min);
If you need to do it inside a template register a helper
Handlebars.registerHelper('getMinPrice', function(value) {
return value.match(/^\$[0-9]*/)[0];
});
var t = Handlebars.compile($('#t').html());
$('body').append(t({
_min_price: "$100.00 - $200.00"
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.1.2/handlebars.min.js"></script>
<script id="t" type="text/x-handlebars">
Before: {{_min_price}}
After: {{getMinPrice _min_price}}
</script>

moment.js will not parse UK format date even when setting the locale

Quite simply, this is my code:
http://jsfiddle.net/NibblyPig/k9zb4ysp/
moment.locale('en-GB');
var d = moment('22/12/2019');
alert(d);
I would expect this to parse, however it says invalid date.
I have referenced moment.js and the locale/en-gb.js
I'm writing a global control so the date may come in in a variety of formats.
If I put in a variety of American dates they all work, for example 12/12/2019, 12/12/2019 23:04 etc.
However the locale command does not appear to do anything and I cannot get a single date to parse. What am I doing wrong?
You need to pass the format as the second argument for moment(), as discussed here:
moment.locale('en-GB');
var d = moment('22/12/2019', 'DD/MM/YYYY');
alert(d);
https://jsfiddle.net/a4gu6kfz/
From the docs:
If you know the format of an input string, you can use that to parse a
moment.
moment("12-25-1995", "MM-DD-YYYY");
I think that there is no need to write your own complex logic to parse your input, you can use moment(String, String) (or moment(String, String[], String, Boolean)), as suggested by Thales Minussi's answer.
moment(String) is the good choice only if your input is in ISO 8601 or RFC 2822 compliant form.
In your case, you can probably use Localized formats listed in the format section of the docs. If you have a list of possible formats, I think that the best choice is tho use moment(String, String[]).
Please note that, by default: Moment's parser is very forgiving, so using default Forgiving Mode will handle "any" character as separator.
Here a live sample:
moment.locale('en-GB');
['22/12/2019', '22/12/2019 15:00',
'22-12-2019', '22-12-2019 15:00',
'1-3-2019', '1-12-2019', '22-1-2019'
].forEach((elem) => {
var d = moment(elem, 'L LT');
console.log(d.format());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/en-gb.js"></script>
Still hoping there's a nice moment js way to do this but in the meantime I just bashed this together. Pretty nasty and it will probably go wrong in 80 years or so.
http://jsfiddle.net/NibblyPig/k9zb4ysp/22/
var a = "23/03/19 12:42:21.123";
var datePart = a.substring(0, a.indexOf(" "));
var timePart = a.substring(a.indexOf(" ") + 1);
var dateParts = datePart.split("/");
if (dateParts[0].length == 1) dateParts[0] = "0" + dateParts[0];
if (dateParts[1].length == 1) dateParts[1] = "0" + dateParts[1];
if (dateParts[2].length == 2) {
var threshold = parseInt(new Date().getFullYear().toString().substring(2)) + 10;
if (parseFloat(dateParts[2]) > threshold ) {
dateParts[2] = "19" + dateParts[2];
}
else
{
dateParts[2] = "20" + dateParts[2];
}
}
alert (parseFloat(dateParts[2] + dateParts[1] + dateParts[0] + timePart.replace(/:/g, "").replace(/\./g, "")));
This won't solve every usecase, but in your specific example if you want just a simple date (with no time component) auto-parsed in UK format you can just use the 'L' format string having set the locale to 'en-GB'
Your example with this change (your jsfiddle also)
moment.locale('en-GB');
// just pass 'L' i.e. local date format as a parsing format here
var d = moment('22/12/2019', 'L');
alert(d);
It's quite nice because you get the auto parsing of various formats you wanted for free. For instance this works just the same:
var d = moment('22-12-2019', 'L');
You can return a date using moment.js in a desired format -
return moment(aDateVar).format('DD/MM/YYYY');

xQuery substring problem

I now have a full path for a file as a string like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
However, now I need to take out only the folder path, so it will be the above string without the last back slash content like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/"
But it seems that the substring() function in xQuery only has substring(string,start,len) or substring(string,start), I am trying to figure out a way to specify the last occurence of the backslash, but no luck.
Could experts help? Thanks!
Try out the tokenize() function (for splitting a string into its component parts) and then re-assembling it, using everything but the last part.
let $full-path := "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
$segments := tokenize($full-path,"/")[position() ne last()]
return
concat(string-join($segments,'/'),'/')
For more details on these functions, check out their reference pages:
fn:tokenize()
fn:string-join()
fn:replace can do the job with a regular expression:
replace("/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"[^/]+$",
"")
This can be done even with a single XPath 2.0 (subset of XQuery) expression:
substring($fullPath,
1,
string-length($fullPath) - string-length(tokenize($fullPath, '/')[last()])
)
where $fullPath should be substituted with the actual string, such as:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
The following code tokenizes, removes the last token, replaces it with an empty string, and joins back.
string-join(
(
tokenize(
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"/"
)[position() ne last()],
""
),
"/"
)
It seems to return the desired result on try.zorba-xquery.com. Does this help?

Resources