DATETIME in Javascript in Database - javascript

I'm trying to store a DATETIME in my Database using Javascript, there is just one problem, it doesn't store the DATETIME which was set to DATETIME in the database.
This is how I store and get the DATETIME,
var currentTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
query('UPDATE `users` SET `claimed`='+currentTime+' WHERE`id`='+pool.escape(user.steamid));
The problem is that the database doesn't update, but it can't be the fault of the database, did I miss something? Can you not store a Javascript "String" as Datetime?
Thanks.

Your Javascript varible is ok.
Try this
query('UPDATE `users` SET `claimed`="'+currentTime+'" WHERE`id`='+pool.escape(user.steamid));

Related

Get timestamp from Firebase and convert it into Javascript date object

firebase realtime database
I manage to fetch the timestamp data from firebase and save it into a variable, currentTime.
The timestamp is saved in string type in firebase as shown in the picture.
I want to convert it into Javascript object type.
So, let jsTime = new Timestamp(currentTime);
And I tried to console.log(jsTime.toDate()) but the console shows 'invalid Date'.
Please help me to solve this problem.
Try it with the following code
let date = new Date(jsTime);

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.

Difference between timestamps value while creating and querying the Sequelize model

I'm having issues with the timestamps on the Sequelize models. When creating a new record via the model, the createdAt timestamp contains milliseconds, but when I query the database using findOne(), it doesn't contain milliseconds. The createdAt column in the database is of type timestamp.
Value while creating model: 2018-12-04T07:18:14.075Z
Value while retrieving model via query: 2018-12-04T07:18:14.000Z
Anyone know what to do? Why is this even inconsistent?
I'm using Sequelize 4.41.2.

How can I store new Date().getTime() in mongodb?

How can I store new Date().getTime() in mongodb? Its being stored as double value.
Example: when I store 1398856176723 , its being stored as 1398856176723.0000 and type as double
Just insert the date object and MongoDB will store it correctly (including timezone information).

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