How to convert MySQL Timestamp to Javascript date? - javascript

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.

Related

Creation of new JSON Object

I am using Google Graphs and I am getting data from MySQL database and encoding it with PHP to JSON and sending it from controller to view. But the problem is date is sending as string and graph cannot use this date. And I changed date to unix system and sending it to view I can get date as a date.
[[1383424123,"AAA",0.001735],[1383424518,"AAA",0.001689],[1383424123,"BBB",0.65211],[1383424518,"BBB",0.655739],[1383424123,"CCC",1],[1383424518,"CCC",1]]
Above I am getting this json object from controller.
In view I am using this json object for Google Graph :
<script> var jsonData=<?php echo $jsdata;?> </script>
I need to get date unix system values and create new date like ->
new Date(jsonData[i][0] * 1000);
And create new the same json object but with new date ( will be replaced with new Date(jsonData[i][0] * 1000); values ) which I am getting from controller and rest of data should remain. How can I do it, creating new json object with replaced date values (only date).
You can just modify the existing value of the array by assigning back to it:
jsonData[i][0] = new Date(jsonData[i][0] * 1000);.

Separating date and time from a Tweet in CouchDB

I'm new using CouchDB harvesting tweets. I'm working with map function to create a view, trying to separate date and hour from doc.created_at of a JSON object by using JavaScript.
This is my code:
function(doc) {
var d= new Date(doc.created_at);
emit(doc._id,d.getTime());
}
The problem is that the time is not user readable. When I use parse, it does not return any value.
I appreciate any advice.

OData Date Filtering from JS

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.

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

How to properly create a JavaScript Array of Dates in Spring?

I've got a Spring Web MVC application where I need to get a JavaScript Array filled with java.util.Dates that are stored in an ArrayList accessible from the webapp with ${cust.dates}.
How do I properly initialize the array in the webapp?
Thank you.
Spring executes at server-side, and JavaScript executes at client-side. For the point of view of Spring, JavaScript is just text that must be generated. And this text must represent valid JavaScript source code.
The JavaScript source code that creates an array of dates could thus be generated like this:
var dateArray = [];
<c:forEach var="javaDate" items="${cust.dates}">
dateArray.push(new Date(${javaDate.time}));
</c:forEach>
This will generate the following JavaScript code:
var dateArray = [];
dateArray.push(new Date(65987985);
dateArray.push(new Date(98654654);
// ...
with the numeric arguments being the number of milliseconds since the epoch, which is the same in Java and JavaScript.

Categories

Resources