how to do date search in mvc core c# when date format is yyyy-mm-dd? - string

I have a grid in .net mvc core that I apply a search in every column in the grid based on a search string. It works on every search string I put in EXCEPT for date strings like yyyy-mm-dd ( ie when hyphens are used).
When I put in yyyy or mm or dd individually the search works. But when I put in yyyy plus the hyphen (ie yyyy-) , it never finds any records.
Here is my c# code for the filter:
if (!String.IsNullOrEmpty(searchString))
{
serviceRequests = serviceRequests.Where(s => s.Id.ToString().ToLower().Contains(searchString.ToLower())
|| s.DateTimeSubmitted != null && s.DateTimeSubmitted.ToString().ToLower().Contains(searchString.ToLower())
|| s.RequestHeading != null && s.RequestHeading.ToLower().Contains(searchString.ToLower())
|| s.RequestDescription != null && s.RequestDescription.ToLower().Contains(searchString.ToLower())
|| s.RequestorId != null && s.RequestorId.ToLower().Contains(searchString.ToLower())
|| s.RequestorFirstName != null && s.RequestorFirstName.ToLower().Contains(searchString.ToLower())
|| s.RequestorLastName != null && s.RequestorLastName.ToLower().Contains(searchString.ToLower())
);
}
How does the filter syntax have to be changed to allow for "yyyy-mm-dd" strings to be used? Why does the hyphen cause problems?
Thanks

It looks like DateTimeSubmitted is of type DateTime. What happens if you use the DateTime.ToString() method? Citing from the documentation:
Converts the value of the current DateTime object to its equivalent
string representation using the formatting conventions of the current
culture.
So, the string representation may not what you expect, e.g. 01.01.0001 00:00:00
I suggest to format the source with a format string like this:
DateTimeSubmitted.ToString("yyyy-MM-dd");
Or another format that fits your needs.

turns out it was a slight twist on suggestion:
|| s.DateTimeSubmitted != null && s.DateTimeSubmitted.Value.ToString("yyyy-MM-dd").ToLower().Contains(searchString.ToLower())
Had to change DateTimeSubmitted.ToString to DateTimeSubmitted.Value.ToString(yyyy-MM-dd) to be able to specify formatting. - ie adding .value before .ToString
Thanks so much!

Related

matching req.body number without quotes

I have the following line of code which run when I send {"key":"1234"} to the server.
if (
(req.path === '/start' || req.path === '/start/') &&
(req.method === 'PUT') && (req.body.key === '1234')
)
However the app I am using sends this content as {"key": 1234} to the server without the quotes around the number and therefore the if statement is getting ignored.
How would I write the if statement above so that req.body.key matches 1234 without the need of the quotes.
Thanks for the help
Use two equals signs rather than three in
req.body.key == '1234'
That will let Javascript convert strings to numbers as it does the comparisons.
Your IDE may whine that you should use three equals signs. Ignore it. You're using two equals signs as intended by the language.
I am not sure if the issue is with how the server accepts the request. However, if it's just a matter of if statements, then you either:
Just leave the single quotes out in the condition req.body.key === 1234
Use == instead of ===, which does not strictly verify the value (including the type) req.body.key == '1234'. Though I wouldn't recommend this, as it this leaves room for errors.

how to compare dates using relational Operators in dart?

I'm working on a problem which requires to compare the date with other if the selected date is less than given then print Hello.
The date is present In String Format like given in Examaple
Ex:
if('2020-1-13'<'2021-1-5')
{
print(hello);
}
You should try this it should work
````
var date1 = DateTime.parse('2020-01-13').millisecondsSinceEpoch.toInt();
var date2 = DateTime.parse('2021-01-15').millisecondsSinceEpoch.toInt();
if(date1 < date2){
print('true');
}else{
print('false');
}
````
Instead of doing that just use the date's isBefore or isAfter operator
It would look something like the following:
final now = DateTime.now();
final yesterday = DateTime.now().subtract(Duration(days: 1));
print(now.isAfter(yesterday)); //true
Since your dates are in STring format, it you would have to use the DateTime.parse() method to construct your date objects, and then use the operators talked about above
package:basics provides DateTime extensions that can easily compare DateTime objects.
(Disclosure: I contributed those particular extensions.)

Hide a webpart using a query string parameter?

I have two web-parts on a same page template and I would like to hide one one of them using a value coming through my query string parameter.
How can I hide a web-part using a query string parameter in Kentico 8 and above?
I am assuming you know how to reach visibility section of the webpart.
Click on the little arrow icon highlighted.
Let's assume the querystring parameter name is cat and you want to show it if it's value is "Visible"
So you can do it like this
{% if( QueryString.GetValue("cat") = "Visible" {true}else{false} #%}
You can also do it in a reverse way like this
**{% if( QueryString.GetValue("cat") != "Visible" {false}else{true} #%}**
Edit:-
You can use this to check multiple values for a single clause like this
if( QueryString.GetValue("cat") != "Visible" && QueryString.GetValue("cat") != "")
You can also use this to combine multiple queries like I did in my case.
if( QueryString.GetValue("cat") != "" || QueryString.GetValue("Author") != "" || QueryString.GetValue("tagname") != "") {true}else{false} #%}
Of course, you can interchangeably use "||" and "&" by tweaking your logic.
I hope this is enough to handle to all your cases. Let me know if it works.

SSIS - if/else expression to turn string into number

I have Excel sheets where the same column can contain a value with % formatting and also with decimal notation.
To clarify: In Excel both have the same formatting but due to different Excel versions (locations) SSIS shows them like this:
1,3% or 0.814260540128523
These values come in as strings so I'm trying to figure out a way to divide the % formatted values by 100 and leave the others as is.
Initially I used:
(DT_R8)[F5_CONV] < 1 ? (DT_R8)[F5_CONV] : (DT_R8)[F5_CONV] / 100
in a second derived column but I then realized that 0,23% is also a possible value.
I think something like this should do it but I'm having trouble with the DT_WSTR and DT_R8 types.
(F5 == "" || ISNULL(F5)) ? NULL(DT_WSTR,40) : FINDSTRING(F5,"%",1) > 0
? (DT_WSTR,40)REPLACE(REPLACE(F5,".",","),"%","") / 100
: (DT_WSTR,40)REPLACE(F5,".",",")
Hope you can help me out here.
Thank you.
(F5 == "" || ISNULL(F5)) ? NULL(DT_WSTR,40) : FINDSTRING(F5,"%",1) > 0
? (DT_WSTR,40)((DT_R8)REPLACE(REPLACE(F5,".",","),"%","") / 100)
: (DT_WSTR,40)REPLACE(F5,".",",")
You can also use script component. Here is a quick solution; you may need to add validations for null. Let us know, if you need help.

Is there any way to retrieve a appended int value to a String in javaScript?

I am currently working on a project that dynamically displays DB content into table.
To edit the table contents i am want to use the dynamically created "string"+id value.
Is there any way to retrieve the appended int value from the whole string in javaScript?
Any suggestions would be appreciative...
Thanks!!!
If you know that the string part is only going to consist of letters or non-numeric characters, you could use a regular expression:
var str = "something123"
var id = str.replace(/^[^\d]+/i, "");
If it can consist of numbers as well, then things get complicated unless you can ensure that string always ends with a non-numeric character. In which case, you can do something like this:
var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
(''+string.match(/\d+/) || '')
Explanation: match all digits in the variable string, and make a string from it (''+).
If there is no match, it would return null, but thanks to || '', it will always be a string.
You might try using the regex:
/\d+$/
to retrieve the appended number

Resources