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.
Related
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.
I'm working on a project in which I need to implement a list of dates. I have been able to pull the data from the api, however I have been having troubles parsing the format within these dates.
The data that I have been able to pull looks similar to this
[["2015-10-05T13:00:00Z","2015-10-05T21:00:00Z"],
["2015-10-06T13:00:00Z","2015-10-06T21:00:00Z"],
["2015-10-07T13:00:00Z","2015-10-07T21:00:00Z"]]
Which is sweet that i've been able to pull.....but as you can see, the dates are not really what a user will need.
I've been looking into moment() methods. (http://momentjs.com/) However the problems that i've run into is that the methods that you can use with moment() is that i've only been able to make anything work with one date, not with an array of dates such as what i have.
So my question is, are there any alternatives to moment(), or better ways of parsing an array of dates?
You'd have to loop and parse.. you can do it fairly simply with a .map call (with momentjs):
var formattedDates = array.map(function(inner) {
return inner.map(function(d) {
return moment(d).format("MM/DD/YYYY hh:mm A");
});
}).reduce(function(p, c) {
return p.concat(c);
});
Demo: http://jsfiddle.net/6ncpspc0/
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!
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);.
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.