1

I'm trying to substract a timeStamp in node js (google cloud functions). This is my timestamp:

var currentTime = admin.database.ServerValue.TIMESTAMP.toString();
console.log(currentTime); //1537806936331

I would like to get the currentTime - 8 hours.

How can I do that? (i'm using it with in a cloud function (firebase))

Thanks in advance for the help & effort!

Karel Debedts
  • 5,226
  • 9
  • 30
  • 70

2 Answers2

4

This timestamp 1537806936331 is equal to Monday, September 24, 2018 7:35:36.331 PM GMT+03:00 DST. You can find here:

https://www.epochconverter.com/

Therefore try the following:

let date = new Date(1537806936331); //Mon Sep 24 2018 19:35:36 GMT+0300 (Eastern European Summer Time)
let hours = date.getHours() - 8; //11

Check here for more info:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date


If you want to get the timestamp of the date above then try the following:

let date = new Date(1537806936331); //Mon Sep 24 2018 19:35:36 GMT+0300 (Eastern European Summer Time) //11
date.setHours(date.getHours() - 8); //11
let timeStamp = Date.parse(date);
console.log(timeStamp); //1537778136000

You can use Date.parse() to get the timestamp.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Thanks! I understand, but is there a function that will return the same format as this: 1537806936331 – Karel Debedts Dec 19 '19 at 13:25
  • u want to convert date to timestamp? – Peter Haddad Dec 19 '19 at 13:26
  • yes indeed, i would like to get the same this 1537806936331 minus 8 hours back in that format. – Karel Debedts Dec 19 '19 at 13:28
  • Why do you want to do that? The timestamp is converted to a date and then u can use getHours which gives you `19` and you substract it – Peter Haddad Dec 19 '19 at 13:29
  • because I need a timestamp in that format for my database... – Karel Debedts Dec 19 '19 at 13:30
  • try the update in the answer – Peter Haddad Dec 19 '19 at 13:43
  • Isn't `date.getTime()` a better way to get the timestamp? `Date.parse()` is for parsing formatted dates. – Barmar Dec 19 '19 at 18:13
  • @Barmar according to this link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse if you give parse() a formatted date then it will return the timestamp for it that's why i used parse(). You can run the code written in the link and then test it here https://www.epochconverter.com/ – Peter Haddad Dec 19 '19 at 18:19
  • But `date` isn't a formatted date, it's a `Date` object. The only reason it works is because the function requires a string argument, and converting a `Date` to a string formats it. So you're formatting it and then parsing it, when you could just use it the way it is. – Barmar Dec 19 '19 at 18:21
  • @Barmar okay I understand thank you for commenting, so I could have done this then `let date = new Date(1537806936331); date.setHours(date.getHours() - 8); let timeStamp = date.getTime(); console.log(timeStamp);` – Peter Haddad Dec 19 '19 at 18:34
  • @Barmar thank you for commenting I really like your answers! – Peter Haddad Dec 19 '19 at 18:34
3

The timestamp you have is the number of milliseconds that have passed since the epoch. This means you can also use a regular subtraction of millisecond to get the timestamp of 8 hours before.

So a simpler version to Peter's (correct) answer is:

var currentTime = admin.database.ServerValue.TIMESTAMP;
var eightHoursAgo = currentTime - 8*60*1000;

Where 8*60*1000 is the number of milliseconds in 8 hours.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807