OData Date Filtering from JS - javascript

I am using the DXTREME framework from Devexpress to connect a HTML mobile app to an OData source.
One of my tables in SQL Server, exposed through the OData service is a table with a date (not datetime) field in it. It is exposed through OData like this:
<d:TaskDate m:type="Edm.DateTime">2010-04-01T00:00:00</d:TaskDate>
I am trying to filter the data on this field through a calendar control, but when I try to filter the datasource on the JS side, I get no matches. This is because the date is passed to the OData service, I believe, in UTC format, so if I query for TaskDate = '10/JUL/2013', I believe the date is passed as "09/JUL/2013 14:00". If I filter on TaskDate > '10/JUL/2013' I get results back from after "09/JUL/2013 14:00" at any rate.
I have tried declaring a new date with no time part:
filterDate = new Date(2013, 6, 10)
but is still doesn't work, it still subtracts 10 formy time zone on the JS side.
What I want to do is to return a lists of Tasks valid on that particular date. How can I achieve this?

I think my problem was the confusion around the dxDateBox control returning just a date, and that date being changed when passed to my odata service.
I solved the issue by converting the date to UTC myself, but just using the Date parts from the control, (where filterDate came from the control):
var paramDate = new Date(Date.UTC(this.filterDate().getFullYear(), this.filterDate().getMonth(), this.filterDate().getDate()));
this.dataSource.filter(["TaskDate", "=", paramDate]);
This works nicely, but seems rather verbose.

Related

Sequelize returning dates with wrong time

The database and node are set as -02:00 timezone.
When I save a register, using sequelize, it saves the register with the right date and time in its date fields. For example, if I save a register with the field moment set as '2017-01-15T23:59:59-0200' and look in the database via MySQL Workbench I will see 2017-01-16 00:00:00 in the respective column.
I can even correctly find registers and filter by date and time.
But the value returned by a find operation in the field is '2017-01-16T01:59:59.000Z', meaning it was added two hours to the answer.
How could I retrive the correct date and time from MySQL using Sequelize?
Solved by overhiding String.toJSON:
Date.prototype.toJSON = function(){ return this.toLocaleString(); }

How to convert MySQL Timestamp to Javascript date?

I want to display a Google Chart (Line Chart) on .jsp page of my Spring MVC application. The data is retrieved from a MySQL database, so I need to convert the YYYY-MM-DD HH:MM:SS format into Javascript's Date.
The database is created by Hibernate. The Reading entity has a time field of type java.sql.Timestamp, which is stored as DATETIME in the database.
The results is an Iterable<Reading> object passed to the .jsp via controller. It is passed correctly (I am displaying the data as a table, too).
I'm trying to use the solution proposed here, but it does not work.
Here's the code I'm trying to populate the chart with:
<c:forEach items="${results}" var="reading">
var t = "${reading.time}".split(/[- :]/);
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
data.addRow([d,${reading.temperature}]);
</c:forEach>
The chart is not displaying.
Facts:
JDBC's java.sql.Timestamp is a subclass of java.util.Date.
JSTL has a <fmt:formatDate> for converting java.util.Date to String.
JavaScript Date constructor can take a.o. a string in ISO8601 time format.
Put together:
<c:forEach items="${results}" var="reading">
<fmt:formatDate var="time" value="${reading.time}" pattern="yyyy-MM-dd'T'HH:mm:ss" timeZone="UTC" />
var d = new Date("${time}");
// ...
</c:forEach>
Alternatively, just convert results to JSON using a decent JSON formatter in the controller and print it as if it's a JS variable like so var data = ${data};. See also a.o. How to access array of user defined objects within <script> in JSP?
Unrelated to the concrete problem: make sure your model is of java.util.Date type. You shouldn't have java.sql.* typed properties in your model. If you're using plain JDBC, just upcast ResultSet#getTimestamp() to java.util.Date directly. See also a.o. Handling MySQL datetimes and timestamps in Java.

new Date(epoch) returning invalid date inside Ember component

I have a date-filter component that I am using in my Ember application that only works on initial render, not on a page reload, or even if I save a file (which triggers the application to live update).
In the main template of my application, I render the date-filter like this passing it a unix timestamp
{{date-filter unixepoch=item.date}}
Then, in components/date-filter.js, I use a computed property called timeConverter to change the unix epoch into a time string formatted according to user's language of choice, and then in my templates/components/date-filter.hbs file I do {{timeConverter}} to display the results
timeConverter: function(){
//step 1: get the epoch I passed in to the component
var epoch = this.get('unixepoch');
//step 2: create a human readable date string such as `Jun 29, 2015, 12:36PM`
var datestring = new Date(epoch)
//do language formatting --code omitted as the problem is with step2
}
It is step 2 that fails (returning invalid date) if I refresh the page or even save the file. It always returns the proper date string the first time this component is called. Even if I do new Date(epoch) in the parent component, and try to pass the result in to this component (to do foreign language formatting), I'm having the same problem.
Question: how can I figure out what's happening inside new Date(epoch), or whether it's an issue related to the component?
I suspect your epoch value is a string (of all digits). If so, then
var datestring = new Date(+epoch);
// Note ------------------^
...will fix it by converting it to a number (+ is just one way to do it, this answer lists your options and their pros/cons). Note that JavaScript uses the newer "milliseconds since The Epoch" rather than the older (original) "seconds since The Epoch." So if doing this starts giving you dates, but they're much further back in time than you were expecting, you might want epoch * 1000 to convert seconds to milliseconds.
If it's a string that isn't all digits, it's not an epoch value at all. The only string value that the specification requires new Date to understand is the one described in the spec here (although all major JavaScript engines also understand the undocumented format using / [not -] in U.S. date order [regardless of locale]: mm/dd/yyyy — don't use it, use the standard one).

How to deal with local/UTC DateTime conversion in Kendo MVC Grid?

I'm getting mad with the Kendo grid. In my ASP.NET MVC project I use a database that contains archive values with a UTC datetime. I want to show these values on my page, but I need the datetime values to be shown as local (browser context) datetime. I also do have two JQuery datepickers for the start/end datetime selection of the desired range that must also work with local time. These two datepickers modify the filters of the grid on each change.
I already managed to change the filter's datetime to UTC by using this code when the datepicker's values change:
var filterExists = false;
$.each($filter, function (index, entry) {
if (entry.field == "DateValue" && entry.operator == "lte") {
entry.value = EndDate.toISOString();
filterExists = true;
console.log("UTCEndDate: " + EndDate.toISOString());
}
})
if (filterExists == false) {
$filter.push({ field: "DateValue", operator: "lte", value: EndDate.toISOString() });
}
When I look into the POST request, the start and end datetimes get send as UTC.
Now the controller fetches the data from the DB like this:
public ActionResult JournalData([DataSourceRequest]DataSourceRequest request)
{
JsonResult jsonNetResult = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
var JournalData = db.DoubleDataValueArchive.ToDataSourceResult(request, x => new JournalEventModel
{
DateValue = x.DateValue,
StationId = x.StationId,
Value = x.Value,
});
jsonNetResult.Data = JournalData;
return jsonNetResult;
}
Using this code, the data gets returned to the grid in UTC. The data is shown in the grid, but the datetimes don't match the values in the datepickers (which show the local datetime).
As I could not figure out a way to convert these datetimes to local time in the grid itself, I tried converting it to local time in the controller by using
DateValue = x.DateValue.ToLocalTime(),
in the code shown before. But now the grid's datetime filter values don't match. Now, when I want to show data for e.g. the last two hours, the most recent hour is not shown when the client's time is UTC+1. This is extremely annoying and I don't know how to resolve this issue.
As far as I can see, sending and returning datetimes to and from the server in UTC would be the most stable way. All conversion should be done on client side and not on the server. It seems that the grid can not work with UTC "under the hood" and display local time whereever it gets rendered on the page.
I hope that somebody ran into this issue before. Thanks for your help!

Query Mongo date field(set by js date) using ruby

Essentially i have a form which takes mm-dd-yy, this value is saved in a database (Value A).
I am using ruby on rails. I am trying to query the database for expired users.
query.push(:expire_date => {:$lt => Time.parse(good_till).utc} )
Problem I am encountering is that the date I pass in the good_till I can convert to UTC using this ruby command, however now i am checking a UTC value to a string.
How do I change my js code in order to save Value A into UTC string to make the two comparable?
I think your :expire_field declaration is incorrect.
Well, it depends on which mongo framework you're using on ruby's app.
Assuming you're using MongoID, you just have to declare your model like this:
class Invoice
include Mongoid::Document
field :expire_date, type: DateTime
end

Categories

Resources