0

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

Clay Banks
  • 4,483
  • 15
  • 71
  • 143
  • 4
    Have you tried [Math.abs()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs)? – StackSlave Dec 19 '17 at 02:04
  • 1
    Why do you have "negative" timestamps in the first place? AFAIK if you are dealing with UNIX timestamps, they should only be positive. – Tim Biegeleisen Dec 19 '17 at 02:04
  • 1
    Just multiply by `-1`, or use unary `-`, if you know that your numbers are always negative? – Bergi Dec 19 '17 at 02:04
  • 5
    "*I do this for sorting purposes, but that's a separate topic.*" - no, it's the cause of your problem. For sorting purposes, you should adjust the sorting method, *not the data*. – Bergi Dec 19 '17 at 02:05
  • @PHPglue I'm trying to avoid converting between strings/integers, Math.abs() returns an integer which I'd then have to convert & yadda yadda – Clay Banks Dec 19 '17 at 02:06
  • @Bergi appreciate the feedback, but I put this method in place to alleviate an even bigger headache. You can message me if you'd like to know more – Clay Banks Dec 19 '17 at 02:07
  • 2
    Bergi is so, so, right. Perfect advice. Don't hack up your data like this. Besides, negative timestamps are perfectly fine and they refer to dates before January 1, 1970 UTC, so this might be a cause for confusion – Ray Toal Dec 19 '17 at 02:07
  • @RayToal understood, I was against this very method from the start but lets assume its staying this way yeah? :D – Clay Banks Dec 19 '17 at 02:10
  • @KuraiBankusu You might want to [ask a new question](https://stackoverflow.com/questions/ask) about how to fix this "headache" that is your actual problem. – Bergi Dec 19 '17 at 02:10
  • Your timestamps are stored as strings? If so, then you should make this clear in your question. Add double quotes around your timestamp values. As for the solution if this is the case, use the `substring` method to exclude the first character. – B. Fleming Dec 19 '17 at 02:12
  • @KuraiBankusu no problem, just reiterating a good suggestion. `Math.abs()` is your friend for now! – Ray Toal Dec 19 '17 at 02:13
  • @B.Fleming they are stored as actual timestamps prefixed with the '-', so I can't utilize `substring()` without converting it first. – Clay Banks Dec 19 '17 at 02:13
  • @Bergi see my update on the post – Clay Banks Dec 19 '17 at 02:19
  • I don't see a "timestamp" type .. https://firebase.google.com/docs/firestore/manage-data/data-types so it must be a number ? – Slai Dec 19 '17 at 02:28
  • `Math.abs(parseInt(yourStringNumberHere))` – StackSlave Dec 19 '17 at 02:30
  • @Slai there is Date and time, if the '-' I prefix to the date converts it to a number I'm not sure yet – Clay Banks Dec 19 '17 at 02:34
  • 1
    yes if you prefix JavaScript Date with a sign it's converted to a number, so either use Math.abs(value) or negate it with -value – Slai Dec 19 '17 at 02:51
  • @Slai looks like that's the only way to go then. Thank you! – Clay Banks Dec 19 '17 at 03:03
  • @KuraiBankusu Thanks, that's pretty horrible. Or was, at least, since they seem to have fixed it and you now can [get the most recent entries](https://stackoverflow.com/a/25647342/1048572) without trickery. In any case, what code are you using to negate the timestamp in the first place? You will need to do the inverse of that to get the original timestamp back. – Bergi Dec 19 '17 at 03:28

0 Answers0