Creation of new JSON Object - javascript

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);.

Related

How to pass a JS date object to cloud function for storing it on Firestore

When I write a Date object to my Firestore, all works correctly and I can see a date in the DB stored with the type "timestamp". If I try to pass a Date object to a cloud function, I don't know why but it is an empty object when I store it from the cloud function to the firestore DB.
firebase.functions().httpsCallable("myFunction")({date: new Date()}) <---- // this doesn't work
So, I have decided to convert the Date object to a firestore timestamp in the client side before sending it to the cloud function. As follows:
date = firebase.firestore.Timestamp.fromDate(date);
firebase.functions().httpsCallable("myFunction")({date})
Then, if I get it in the cloud function and store it on my firestore, I see an object with the fields "seconds" and "nanoseconds", but not a timestamp (I mean it is not stored with the type Timestamp)... So when I get this date I cannot apply the method .toDate()
Is there any good way to store the date in this situation as a timestamp and not as an object?
The input and output of callable functions deal entirely with JSON, so they are limited to expressing only those types that are allowed in JSON: string, number, boolean, null, object, array. Date and Timestamp are not among those types. What the Firebase SDK will do is try to serialize custom objects as JSON, which is not working the way you want - the seconds and nanos fields of the Timestamp are being split up during serialization.
If you have a Date to send from the client, you should just send it as a number. Instead of passing a Date object, pass the single number inside it (milliseconds since unix epoch):
const number = date.getTime()
On the receiving side, in Cloud Functions, take the number and convert it back to a Date:
const date = new Date(number)
Then you can write the Date object to a Firestore document field.

get date from firebase format change

earlier we push date use native Firebase format
createAt:admin.firestore.Timestamp.fromDate(new Date)
so data store like this
"createAt": {
"_seconds": 1563181560,
"_nanoseconds": 567000000
}
new we change like this
createAt:new Date().toISOString()
now store like this
"createAt": "2019-07-17T07:17:05.115Z"
buts some date store like this because we use native date format
now how can i call old date to new date format? moste of the data sote like native date method is that any way to change new method?
The Firestore Timestamp type has many conversion methods. Just like it has a fromDate() it also has a toDate() method. So if you reload the data from Firestore and get a Timestamp object, you can then get a date with timestamp.toDate() and thus an ISO-8859 formatted string with timestamp.toDate().toISOString().

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.

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.

Categories

Resources