I have some entries in a database and and they all contain a date string.
I have some TS like this
ref.LimitToFirst(10).orderByChild('date')
The problem i an having is that TS treats this as a regular string and just orders by the first number in the date. ex - 10/1/2021 would be ordered before 2/1/2021. How could i fix that ?
You can't fix this in code while depending on the database to perform the query. In Realtime Database, strings will always sort lexicographically - by their natural string order. You can't make it interpret the strings as something other than a normal string.
You will have to instead store an integer value that you can sort chronologically. In JavaScript, you can simply create a Date object and use its getTime() method field to get the Date's representation in millis since the unix epoch.
If you can't store a proper timestamp, then your only option is to read all the data and sort it yourself in your app, which will not scale well.
Related
I'm trying to scan a table and return all entries which match my filter expression. I've got an API Gateway which accepts a GET-Request that contains a starting time and a end time. (Format: [dd.mm.yyyy hh:mm:ss]
The database im scanning has an attribute called "timestamp" in the same format. I've tried it with the filter
Timestamp BETWEEN :date1 and :date2
date1 and date2 represent the starting and ending time from the API. Timestamp is the field in my Database.
Unfortunately the scan doesnt return any values.
Thanks a lot.
Your date format has to be in ISO 8601 string format for you to be able to scan on it. You can see this in the documentation here, under the "String" category.
I have a javascript date string in ISO format, e.g
dtStart = 2016-09-05T07:00:00.000Z
and I want to compare it in an SQL query to a column that is label DateTimeCollected. An example of DateTimeCollected could be 2014-06-27T00:00:00.000Z.
How could I comare these two dates in SQL by using something like
DateTimeCollected >= ${dtStart}
in the query where dtStart is a string. I am using MSsql if that is of any help.
DateTimeCollected >= CONVERT(${dtStart}, #stringDate, 126)
If you are comparing the values in Sql, you should be able to simply do this:
DateTimeCollected >= CAST(#DateTimeString As DateTime).
Assuming #DateTimeString is a char(23) variable containing a string representation of a datetime value following the ISO8601 format (yyyy-mm-ddThh:mi:ss.mmm)
In fact, you can also do this without the casting, because Sql server will implicitly cast the string into a datetime data type. However, I beleive it's better to use explicit casting since it's more readable.
As I wrote in the comment, SQL Server should always cast the correct date if it's passed as a string following the ISO8601 format.
I need to insert to database two variables:
- "start" with value "9:00"
- "end" with value "20:00"
It can be types String or Timestamp, i would prefer the string. But in database table the fields are "Time" type. So how to cast it? I got only time values without full date like month, days and years.
From what I have read, if you want to insert a string into a MySQL TIME column, it will have to have the format HH:mm:ss. So the easiest solution for you might be to obtain your start and end times in the format of 09:00:00 and 20:00:00.
I expect the following INSERT statement to proceed without error:
INSERT INTO yourTable (`time_from`, `time_to`)
VALUES
('09:00:00', '20:00:00')
To be clear, I would recommend putting in a little effort in your code to obtain time data in this format. Then just insert it into MySQL without further hassle.
In a Date Field you van only Store Dates without time Information.
So you Must change the fieldtype or add a nee Field for this
So, I just had a big "Oh Snap!" moment.
I'm using MomentJS with AngularJS and IndexedDB in an offline app.
All my records are stored with an index date_idx based on the date property of the object.
The problem comes when I try to run queries based on the date using the following code:
$scope.redoresults = function(){
$indexedDB.openStore('records', function(store){
$scope.upper = moment($scope.dates.endDate).format("DD[/]MM[/]YYYY");
$scope.downer = moment($scope.dates.startDate).format("DD[/]MM[/]YYYY");
var find = store.query();
find = find.$between($scope.downer, $scope.upper, false, false);
find = find.$index("date_idx");
store.eachWhere(find).then(function(e){
$scope.daterange = e;
console.log($scope.daterange);
});
});
};
The query was delivering weird results, sometimes correct, sometimes not.
Until I realized that I'm actually querying against strings, not dates.
Being so, a query between 01/08/2016 and 31/08/2016, will still deliver me results containing 20/09/2016, because as a number it's indeed between the range.
I feel I'm missing something structure-wise.
Any comment on how to solve this issue?
Should I query against some other index? But then how can I compare the dates?
Store dates as dates, and query using dates.
Or, store dates as timestamps (e.g. date.getTime()), and then query using timestamps
Do not store dates as dates and then query with a string date, this will produce junk.
Do not store dates as strings, and then query with a string date, this will produce junk. String values are compared lexicographically which can easily depart from how dates are compared.
I have a JSON output from a non-configurable system where various date/time variables have values like \/Date(1422691756316)\/
How can I get that into a readable format (I need to display it via PHP and Javascripts).
You could deserialize the JSON output via json_decode() and then you could try to parse the string to an timestamp with strtotime (http://php.net/manual/de/function.strtotime.php), then you could create a date from thins timestamp using the date() method (http://php.net/manual/de/function.date.php) with a given formatting string an the timestamp.
But I'm not sure if your date string is ready to be parsed. In all cases you may to remove the \/Date( and )\/ parts of the string.
EDIT: Looks like your string is not a timestamp, strtotime fails. You may have to check what kind of timestamp/ datestamp/ whatever your string is.