Formatting data for an API JSON response - json-api

I have a couple of questions about formating data ready for an API JSON response.
If sending a datetime value (YYYY-MM-DD HH:MM:SS format) in an API json response, if there is no datetime set, which one of these should it be:
an empty string ""
A string of "0000-00-00 00:00:00"
null
Is it good practice to make all values have a fixed datatype (not a mixed data type) in API responses? So for example, if a value will be an array, but there are no elements in the array, should the value be made an empty array [] rather than false or null so that value will always be an array?

you can use null data type
You can use a nullable datatype e.g. DateTime? MyNullableDate;

Related

Cloud Functions Query Date from Firestore field which is a String

I created a Firestore Database all fields are in String Format.
Now I need to by date on this field (reqDate) which is in string format.
Sample Data on the field (reqDate) = 10/11/2021, 9:22:11 PM
I tried the code below on Cloud Functions using moment to format the string field:
const thisCollection = firestore.collection("ItemList")
.where(moment("reqDate").format('MM/DD/YYYY'), ">", "10/05/2021");
But it return blank or no record.
Thanks in advance
First off, the condition you pass to you query is wrong because you need to pass:
The field name to filter on in the first parameter, which is now moment("reqDate").format('MM/DD/YYYY'), but should be "reqDate".
The operator, which is correct in your code.
The value to filter on.
This date format you use is unsuitable for range filters, since strings are ordered lexicographically and in that order: "10/05/2021" is before "11/05/2020".
To allow a range query, you need a date format that allows for that. When the date is a string, the most common formats are based on ISO-8601, such as "2021-10-17", because in this format dates that are after October 17 are queryable with: .where("reqDate", ">", "2021-10-17")

Reading dates from Sqlite with Delphi / Firedac

When reading date fields from Sqlite into Firedac, I get conversion errors. The fields are called dates but with string entries (yyyy-mm-dd). I set the option for Datetime Format = string, but I've discovered that while null values are handled OK, empty values (= '') produce an error which I can't figure out how to handle.
You can enable StrsEmpty2Null option, which will automatically convert all empty strings to NULL state. But it's for all values and parameters handled by the data component. So it's not the cure.
I'm not sure what you're doing, but in general, NULL is a state and you cannot convert NULL state to a value because it's a state indicating no value. So as you cannot convert empty string to date.
So try to describe more about your value to string conversion, so we can suggest a proper way to deal with it. For SQLite, I'd suggest using DATE pseudo data type and convert values through the built-in formatting expressions.

DateTime Type Gets Converted into bigint in SQLite

I Am new to SQLite.I am trying to create table from my model class in which I have DATETIME field.when sqlite table creatd from This Class The DATETIME column creted of type bigint.I know the reason because SQLite doesn't support DATETIME
So what should I do to create Column Of type TEXT in SQLITE without Chnging DATETIME type in my model Class. I found some post regarding to this but don't get satisfactory solution.please help.Thanks
You can configure how DateTimes are stored in your connection string. Change the attribute DateTimeFormat to either "ISO8601" or "CurrentCulture".
But: I would not recommend to do that. If you ever want to sort by a datetime or you want to filter rows (give me all entries from the last 2 weeks) then the bigint approach is the most efficient one. While ISO8601 datetime strings are sortable, that is most likely not the case with localised CurrentCulture strings. So if you really want human-readable strings in your database then choose the ISO version.

CLR DateTime object comparison with SQL Server DateTime

I'm using this implementation to conduct server side searching using Entity Framework for jqGrid. The issue that I'm having is that although the search is working fine for text or numerical fields, searching using DateTime values isn't working.
The problem is that the DateTime object in my model class sends the string representation of the object (i.e. in the format 2/9/2014 12:00:00 AM) to the database but the database is formatted as 2014-09-03 00:00:00:000. As a result, the comparison always fails.
I can't change my DateTime property to a string so I'm stumped. The resultset is returned via a stored procedure (a simple SELECT * FROM [TableName]) so I tried formatting the associated Date field and returning that but it returns as an nvarchar.
Has anyone encountered this before or have any recommendations as to how to resolve this issue? I'd appreciate any help, thanks!
Just to provide an answer for anyone who comes across this. I took the following steps to resolve this issue:
1) Made the following change in the JQGrid support class:
_formatObjects.Add(parseMethod.Invoke(props[rule.field], new object[] { rule.data }));
to
_formatObjects.Add(parseMethod.Invoke(props[rule.field], new object[] { (parseMethod.ReturnType.FullName == "System.DateTime" && rule.data != "") ? Convert.ToDateTime(rule.data, CultureInfo.CreateSpecificCulture("fr-FR")).ToString() : rule.data }));
2a) In my controller, I added the following bit of code whenever I was fetching directly from the table (you have to specify the column names explicitly to truncate the time portion):
//For matching date instead of datetime values
if (wc.Clause.Contains("Date"))
{
wc.Clause = wc.Clause.Replace("DeliveryDate", "DbFunctions.TruncateTime(DeliveryDate)");
}
results = results.Where(wc.Clause, wc.FormatObjects);
2b) If the data was being returned from a stored procedure, I just returned an appropriate Date field from the SP (this approach will work for DateTime fields only if the timestamp portion is all zeros).
Hope this helps someone else.

How to change date format of a datetime object?

currently, i have a datetime object
DateTime theDateTime = DateTime.ParseExact(dateAndTime, "d MMMM yyyy hh:mm tt", provider);
which successfully converts it into a datetime (from a string) to become for example :
7/6/2012 9:30:00 AM
How do i convert this to become 2012/07/06 09:30:00 (24hr format)? So that i can insert it into the database using C#??
PS: I'm using Sybase SQL Anywhere 12, and from what I've read, they neeed the format to be in year/months/day and the time to be in 24hr format right? Please correct me if I'm wrong.
The DateTime itself does not have a format. The date and time are stored internally as a number. Usually the classes of the database provider take care of converting a DateTime to the correct format.
If Sybase will only accept the date formatted as a string you will need to use the DateTime.ToString method and format it with the correct format string.
How are you building your insert command? Are you using database parameters or just building a string containing the insert statement?
SQL Anywhere 12 has a default date format of YYYY-MM-DD HH:NN:SS.SSS
This can be configured/changed with the timestamp_format database option however:
timestamp_format option
The setting can be permanently changed through SQL like:
SET OPTION PUBLIC.timestamp_format = '<format here>';
Or temporarily changed (per connection basis) like:
SET TEMPORARY OPTION timestamp_format = '<format here>';
Of course, if you already have a datetime object in your code, you should be able to pass the value into a parameterized query. It doesn't have to be passed as a string.

Resources