Want to sort this object by date in dart
I try use build in method .sort but it's not correct
For this you have to convert date string into Datetime.
Here is how you can sort:
To sort in ascending order you can use:-
list_name.sort((a, b) => (DateFormat('yyyy-MMM').parse(a).compareTo(DateFormat('yyyy-MMM').parse(b))));
To sort in descending order use - in this:-
l.sort((a, b) => -(DateFormat('yyyy-MMM').parse(a).compareTo(DateFormat('yyyy-MMM').parse(b))));
Related
I have successed to extract max value between two date that I have saved with sharedpref, but i don't know how to calcul if I add some dates between two previous date. it's simple if I have a little of data to compare but if I have a lot, I don't know how to compare each to extract max value between all posibilities.
List.reduce and DateTime.isAfter might come in handy.
final maxDate = dates.reduce((a,b) => a.isAfter(b) ? a : b)
you can use following function which receives list of dates and return max (latest) one:
DateTime calcMaxDate(List<DateTime> dates){
DateTime maxDate = dates[0];
dates.forEach((date){
if(date.isAfter(maxDate)){
maxDate=date;
}
});
return maxDate;
}
Using Realm 2.7.0 and Swift 3, my application tries to filter a object based on its date, returning all dates that are NOT between two given dates.
Given a Object called Model, it has an variable called date (type NSDate).
Currently, I am using this code to filter:
realm.objects(Model.self)
.filter("NOT date BETWEEN %#", [today, twoDaysAgo])
But it is returning all data, the filter is not working.
Thank you.
Try this
realm.objects(Model.self)
.filter("NOT (date BETWEEN %#)", [today, twoDaysAgo])
try this predicate query
let predicate = NSPredicate(format: "!(date BETWEEN %# , %#)", today, twoDaysAgo)
I have an RDD in spark which is essentially (timestamp, id), where the timestamp is joda DateTime of the form yyyy/MM/dd HH:mm. The RDD is of class;
case class myRDD(timestamp: org.joda.time.DateTime, id: String)
I am using Spark and Scala.
I want to filter the data to only have a certain day i.e. 2000/01/01, and return something of the form (timestamp, id), but am unsure how to use filter() with the joda timestamp. I have created the start and end of the interval I want to filter by the following;
val start = myFormat.parseDateTime("2000/01/01 00:00")
val end = myFormat.parseDateTime("2000/01/02 00:00”)
but I do not know how to apply this to an RDD, or even if this is the best way to approach this. Any tips would be greatly appreciated.
For just 1 day:
rdd.filter( (timestamp, id) =>
timestamp.withTimeAtStartOfDay.equals(dayYouWant.withtimeAtStartOfDay) )
For a range of days:
rdd.filter( (timestamp, id) =>
new Interval(start, end).contains(timestamp) )
assuming I have the following nested document structure, where my document contains nested routes with an array of date time values.
{
property_1: ...,
routes: [
{
start_id: 1,
end_id: 2,
execution_times: ['2016-08-28T11:11:47+02:00', ...]
}
]
}
Now I could filter my documents that match certain execution_times with something like this.
query: {
filtered: {
query: {
match_all: { }
},
filter: {
nested: {
path: 'routes',
filter: {
bool: {
must: [
{
terms: {
'routes.execution_times': ['2016-08-28T11:11:47+02:00', ...]
}
},
...
]
}
}
}
}
}
}
But what if I would like to filter my documents based on execution dates. What's the best way achieving this?
Should I use a range filter to map my dates to time ranges?
Or is it better to use a script query and do a conversion of the execution_times to dates there?
Or is the best way to change the document structure to contain both, the execution_date and execution_time?
Update
"The dates are not a range but individual dates like [today, day after tomorrow, 4 days from now, 10 days from now]"
Well, this is still a range as a day means 24 hours. So if you store your field as date time, you can use leverage range query : from 20-Nov-2010 00:00:00 TO 20-Nov-2010 23:59:59 with appropriate time zone for a specific day.
If you store it as a String then you will lose all the flexibility of date maths and you would be able to do only exact String matches. You will then have to do all the date manipulations at the client side to find exact matches and ranges.
I suggest play with range queries using Sense plugin and I am sure it will satisfy almost all your requirements.
-----------------------
You should make sure that you use appropriate date-time mapping for your field and use range filter over that field. You don't need to split into 2 separate fields. Date maths will allow you to query just based on date.
This will make your life much easier if you want to do aggregations over date time field.
Reference:
Date Maths:
https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math
Date Mapping : https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
Date Range Queries:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
I have a DateTime field in a MongoDB collection. I can't find a way to orderby just the date portion of the DateTime field. SortOrderDate is a DateTime field but I want to sort just the Date portion of the DateTime field. I'm using C# and it's native MongoDB driver.
var collection = MongoDatabase.GetCollection<ISummary>("summary");
var items = (from e in collection.AsQueryable()
where e.SortOrderDate >= today.Date
&& e.SortOrderDate < today.AddDays(1).Date
orderby e.SortOrderDate descending, e.SortOrder ascending, e.Views descending
select e).Skip(0).Take(count).ToList();