Get the Positive Value of a "Negative" Timestamp - javascript

Simple Date question here, but I can't seem to find a simple answer.
I am storing the 'negative' value of a timestamp in my database like so (they are stored as numbers, not strings):
user-1
--date: -1495255666921
user-2
--date: -1495255666341
I do this for sorting purposes, but that's a separate topic.
When it comes time to read these dates under each user, I want to get the 'positive' version of the timestamp.
I know I can toString() the date, check for the - character, remove it, then convert it back to a Date() (which I then have to run through another function), but that seems inefficient when sending thousands of records back to the client.
Is there a magical way I can poof - get rid of that - and maintain it's datatype as timestamp programatically? Any input or recommendation is appreciated!
UPDATE - Since multiple people have mentioned, this is is the 'bigger' issue I'm dealing with and why I've chosen this sorting method

Related

JavaScript Date Object includes "T" character in browser, but not in MongoDB

I've seen this post already, and it didn't quite shed much light on what is going on.
I have a mongo server, that stores information about classes, what times and days they meet and what not. I also have an express server, that interacts with and returns this data.
When I look at the data in Robo3T (a mongo data viewer), it is formatted - as it should be - like so:
2018-09-07 04:00:00.000Z
But when the data comes back to the server, it comes back like this: 2018-09-07T04:00:00.000Z
What is causing this? In no way do i make any attempt to format this data before I display it. I also checked the server output, which returns it the same way:
I guess my question is, why is this happening? and is this impacting my ability to query results from mongo based on a date range? Any assistance would be much appreciated, thank you.
Robo 3T just happens to display dates that way. This does not mean that it actually is stored that way. When you switch the view to "text mode", you will see that it actually is ISODate("2018-09-07T04:00:00.000Z") (with the T).
This format is actually the date format string that is used in JavaScript. And yes, the T is required. See What are valid Date Time Strings in JavaScript?

Time zone issue when using REST Api and JS

I'm using FormatJS library along with Handlebars to display a list of events that occured in the past. I'm calling for an endpoint on my server's REST API which returns me the list of events in Json, with datetimes to display for each event. ATM I'm saving datetimes in the DB using GMT time zone.
So when I'm getting my Json, I'm handling datetimes like this :
{{formatRelative commentDate}}
My issue is, since the datetimes are stocked in GMT, they display also like that. For example, since I'm on a GMT+2 timezone, as soon as a new event is created and shows up on the list, I see it "happened 2 hours ago" while it should be "a few seconds ago".
So, is there a way I can handle this ? Am I making a mistake in saving datetimes in GMT in my DB, and if so, how would you handle datetimes coming from different timezones and displaying them to people in other timezones ?
Of course I could customize the formatRelative helper to play with getTimezoneOffset and get the wanted result, but I wanted to know if there is something better to do.
Thanks a lot ahead !
The key to understanding your question is what you wrote in the comments:
Getting the Json, containing datetimes in the format 2016-02-28 10:15:53 - that's UTC time
You should ensure the value in JSON is in full ISO8601 format, including the appropriate offset or Z character to indicate UTC: 2016-02-28T10:15:53Z
Without the offset, most implementations will consider the value to be represented in local time, which explains your results.
Thus, the problem is with your server-side code, not your JavaScript code. There may be a client-side workaround you could apply when the date string is parsed from JSON, but really the best solution would be to qualify it at the server.

Is there a way to query nodes in Neo4j based on "dateProperty"

I have a group of nodes living in Neo4J with a label: Image.
These nodes have three properties. ID, imgPath and Time_taken.
I am writing a Javascript function in which the user can select dates in two textboxes (start/end date) and then on button click a PHP script runs with a neo4j request.
I read that neo4j doesn't support date format. My question is if there is a straightforward way to query nodes based on a "date property". Please note that the date is not the same with the timestamp of the node's creation.
Thanks
D.
I suggest you take a look at GraphAware TimeTree, which allows you to attach events (Images in your case) to an in-graph time index and query by time, time ranges, etc.
In the next release (which will be out soon after Neo4j 2.2 is out), you will be able to configure the tree to automatically attach Images to the tree without you as a programmer needing to do the attachment explicitly.
This is the workflow I followed after all:
1) I used the datePicker of JQuery to select dates.
2) I found all the dates between the start and the end date.
3) I played with the strings and created the query in a suitable form.
In the query I used the LIKE in order to find similarities related with this part of the date: day Month Year (e.g. 12 Feb 2015).
4) I send the request using CURL (and PHP).
5) Got the results in JSON format.
So to sum up what actually helped me to solve the problem was to find the dates (using javascript/jquery) between a start and an end date and then use the LIKE functionality.
I hope this helps someone. I will post the code when I find some time.
D.

Return formatted value in MongoDB db.collection.find()

I have a MongoDB JavaScript function saved in db.system.js, and I want to use it to both query and produce output data.
I'm able to query the results of the function using the $where clause like so:
db.records.find(
{$where: "formatEmail(this.email.toString(), 'format type') == 'xxx'"},
{email:1})
but I'm unable to use this to return a formatted value for the projected "email" field, like so:
db.records.find({}, {"formatEmail(this.email.toString(), 'format type')": 1})
Is there any way to do this while preserving the ability to simply use a pre-built function?
UPDATE:
Thank you all for your prompt participation.
Let me explain why I need to do this in MongoDB and it's not a matter of client logic at the wrong layer.. What I am really trying to do is use the function for a shard bucketing value. Email was just one example, but in reality, what I have is a hash function that returns a mod value.
I'm aware of Mongo having the ability to shard based on a hashed value, but from what I gather, it produces a highly random value that can burden the re-balancing of shards with unnecessary load. So I want to control it like so func(_id, mod), which would return a value from 0 to say 1000 (depending on the mod value).
Also, I guess I would also like to use the output of the function in some sort of grouping scenario, and I guess Map Reduce does come to mind.. I was just hoping to avoid writing overly complex M/R for something so simple.. also, I don't really know how to do Map Reduce .. lol.
So, I gather that from your answers, there is no way to return any formatted value back from mongo (without map/reduce), is that right?
I think you are mixing your "layers" of functionality here -- the database stores and retrieves data, thats all. What you need to do is:
* get that data and store the cursor in a variable
* loop through your cursor, and for every record you go through
* format and output your record as you see fit.
This is somewhat similar to what you have described in your question, but its not part of MongoDB and you have to provide the "formatEmail" function in your "application layer"
Hope it helps
As #alernerdev has already mentioned, this is generally not done at a database layer. However, sometimes storing a pre-formatted version in your database is the way to go. Here's some instances where you may wish to store extra data:
If you need to lookup data in a particular format. For example, I have a "username" and a "usernameLowercase" fields for my primary user collection. The lowercase'd one is indexed, and is the one I use for username lookup. The mixed-case one is used for displaying the username.
If you need to report a large amount of data in a particular format. 100,000 email addresses all formatted in a particular way? Probably best to just store them in that format in the db.
If your translation from one format to another is computationally expensive. Doubly so if you're processing lots of records.
In this case, if all you're doing is looking up or retrieving an email in a specific format, I'd recommend adding a field for it and then indexing it. That way you won't need to do actual document retrieval for the lookup or the display. Super fast. Disk storage space for something the size of an email address is super cheap!

converting time using strtotime PHP

apologies this is probably a simple question - I am using php and JS to create a counter and I have stored a time using an SQL time field in my DB- I run a query to return this time but I now wish to convert this time into an integer, I assume I need to use the following, but I am lost on the syntax:
http://www.electrictoolbox.com/using-strtotime-with-php/
any help would be appreciated. many thanks in advance
It's hard to tell from your question what exactly you mean by "an SQL time field."
MySQL offers four kinds of time-like data types: TIMESTAMP, DATETIME, TIME, and DATE. To some extent it matters which one your table contains.
It's also hard to tell what you mean by "convert this time into an integer." Do you mean a UNIX-style timestamp (number of seconds since 1-Jan-1970 00:00:00 UTC)?
If that's the case you can get your database to do this conversion: use
SELECT UNIX_TIMESTAMP(value) ...
in your query. That will work for all the time-like datatypes except TIME. (Strange, eh? TIME means time of day with no date, so that's why it doesn't work.)
Otherwise, without more information it's hard to tell you how to use php to do this: we can't tell what your converting from and what you're converting to.

Categories