Why can't I bind dates in query string with GET parameters? - ajax

I am using ASP.NET Core 3.1. I am trying to bind dates in a query string to a PageModel using GET. Here is my query string:
?id=15+startDate=1900-01-01+endDate=1900-01-01
Here is the signature of my OnGet:
public void OnGet(int id, DateTime startDate, DateTime endDate)
The values are always 0, 1/1/0001 00:00:00, 1/1/0001 00:00:00 respectively.
If I remove the dates and just pass the id, it works fine. Why do the dates in the query string break everything including the id? I have also tried changing the data types from DateTime to string and it still breaks with the string parameters always being null.

The separator for parameters in a query string is &, not + as your example has. Corrected:
?id=15&startDate=1900-01-01&endDate=1900-01-01
An arguably better approach with datetime parameters is to accept them as strings, then use DateTime.TryParse to parse with control over expected format. That way you'll always have the opportunity to inspect the string value provided and decide if you want to convert it to a DateTime, and how.

Related

Select Datetime as Date in criteria query

I'm using JPA CriteriaBuilder to select attributes from the database. I have a specific use case that involves Conversion of datetime to date. The Projection segment of the code is as follows.
CriteriaQuery<ResponseDTO> query = cb.createQuery(ResponseDTO.class);
query.multiselect(root.get(MasterPackagesV1_.masterPackageName),
packageJoin.get(PackagesV1_.packageId),
packageJoin.get(PackagesV1_.packageName),
packageJoin.get(PackagesV1_.startSellingDate),
packageJoin.get(PackagesV1_.endSellingDate),
packageJoin.get(PackagesV1_.createdDate),
packageJoin.get(PackagesV1_.modifiedDate),
packagePaymentJoin.get(PackagePaymentPlanV1_.mrp),
packageSubscriptionJoin.get(PackageSubscriptionsV1_.packageSubscriptionId)
);
All of the date attributes are stored as datetime datatype in the MySQL database. I'm want the datetime to be formatted to date. i.e Instead of retrieving the Dates as LocalDateTime I want it to be converted to LocalDate during Instantiation.
I have made changes in the ResponseDTO class accordingly to support LocalDate. Thanks in Advance.

How to return a query from cosmos db order by date string?

I have a cosmos db collection. I need to query all documents and return them in order of creation date. Creation date is a defined field but for historical reason it is in string format as MM/dd/yyyy. For example: 02/09/2019. If I just order by this string, the result is chaos.
I am using linq lambda to write my query in webapi. I have tried to parse the string and try to convert the string. Both returned "method not supported".
Here is my query:
var query = Client.CreateDocumentQuery<MyModel>(CollectionLink)
.Where(f => f.ModelType == typeof(MyModel).Name.ToLower() && f.Language == getMyModelsRequestModel.Language )
.OrderByDescending(f => f.CreationDate)
.AsDocumentQuery();
Appreciate for any advice. Thanks. It will be huge effort to go back and modify the format of the field (which affects many other things). I wish to avoid it if possible.
Chen Wang.Since the order by does not support derived values or sub query(link),so you need to sort the derived values by yourself i think.
You could construct the MM/dd/yyyy to yyyymmdd by UDF in cosmos db.
udf:
function getValue(datetime){
return datetime.substring(6,10)+datetime.substring(0,2)+datetime.substring(3,5);
}
sql:
SELECT udf.getValue(c.time) as time from c
Then you could sort the array by property value of class in c# code.Please follow this case:How to sort an array containing class objects by a property value of a class instance?

Laravel 5: can't set date from string

On my db migration i have a "dob" field:
$table->date('DOB')->nullable();
I have a string representing a date: "12/12/1960" in the input. I tested "dob" and the content is there. But when I try to set it to the "dob" field in the database..
$member->DOB = date('m/d/Y',Request::input('dob'));
The datebase field becomes 0000-00-00
What am I doing wrong?
The database most likely wants it in YYYY-MM-DD format. So just changing the format should work. But to get from 12/12/1960 to there, you need to parse it.
$oldDateString = '12/12/1960';
$newDateString = DateTime::createFromFormat('m/d/Y', $oldDateString)->format('Y-m-d');
Look at definition
string date ( string format [, int timestamp] )
Second parameter is timestamp, not string
Use DateTime PHP5 functions or Laravel Carbon
E.g. date_create_from_format('m/d/Y', Request::input('dob'))
->format('Y-m-d H:i:s');

Extract only date part in a datetimein LINQ

We have some data in a DataTable and we are using the query like this to get what we need.
IEnumerable<Results> subResult = from query in datatable.AsEnumerable()
select new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date")
}
This above query returns what i need but date in full format (Ex: m/dd/yyyy hh:min:sec am/pm) but we need only date part of it(only mm/dd/yyyy need to be pulled in). When looked in the properties of this, couldn't find an implicit way to get it, please help me in getting this result. Thanks.
The DateTime class has all the properties you just mentioned. You should try to fix your display with the ToString("anyformatyouwant") formats.
What about this?
Date = query.Field<DateTime?>("Date").Date
This will give you just the date part:
IEnumerable<Results> sub-result= from query in datatable.AsEnumerable()
where new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date").Date
}
However if you just want to display the date somewhere (meaning you're not using this to do date grouping, etc. then I would just specify the display format in your UI layer.
AFAIK there is no Date Class in .net, if you want pure date you should write a Class for that, otherwise you can pull out the part as a string
DateTime.Now.Date.ToString("yyyy/MM/dd")
gives you the current date in string type, but if you get the .Date part it gives you a DateTime object which still has a time part showing AM 12:00:00:0 blah blah

Query formatted value using LINQ

I have to query a Date column with formatted values. Consider a scenario, same date with different timing "05/12/2012 02:00:00" and "05/12/2012 12:00:00". I have to query this collection such that resultant should have only date and not time "05/12/2012"(dd/MM/yyyy).
datasource.AsQueryable().Select("Date").Cast<object>().Distinct().ToList<object>();
I have used the above to get collection and i am not able to get the formatted collection.
Thanks.
Why not just juse the Date property of DateTime?
datasource.Select(d => d.Date.Date).Distinct()
I can't figure out what you're trying to do with the Cast.

Resources