How to Get Server Timestamp Using Firebase Cloud Functions

In this tutorial, we are going to create a Cloud Function which will return the server timestamp.

Getting the Firebase Server Timestamp

If you only care about the timestamp and not what format it comes in the result, this method is for you. You can simply create an endpoint in your https API which will result in you getting whatever the time is on the server.

1
2
3
4
5
6
const functions = require('firebase-functions');
const timestamp = (req, res) => res.status(200).send(new Date());

module.exports = {
api: functions.https.onRequest(timestamp),
};

This will result in a response like this:

1
"2018-05-10T10:13:54.615Z"

If you have ever dealt with Dates in Javascript, you’ll quickly realize that this format is a little ugly. This is because it is a little hard to manage dates with offset values correctly.

Regardless, this can get the job done if you don’t need anything special.

Here’s a slightly modified version of the same function with a check for req.method. We are not allowing anything else than a GET request. This is a little more REST API standard friendly. This is because we are only allowing the GET method for a request which should ONLY be a GET request and nothing else.

1
2
3
4
5
6
7
8
9
10
11
12
13
const functions = require('firebase-functions');

const timestamp = (req, res) => {
if (req.method !== 'GET') {
return res.status(405).send(`${req.method} method not allowed`);
}

return res.status(200).send(new Date());
};

module.exports = {
api: functions.https.onRequest(timestamp),
};

What I did here is nothing special. I just added a check for the request method which should logically be a GET request as per the REST standards.

Formatting the Firebase Server Timestamp

Chances are that you want the timestamp to be formatted in a specific way from the request. Again, since the Cloud Functions run on top of a standard Node environment, we can achieve that too.

For this task, we can use the moment package from npm.

Navigate to the functions directory of the Cloud Functions code and install moment.

1
npm install moment

With the help of moment, you can handle almost all of the common date/time formats.

After adding the moment library to our code, it will look like as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const functions = require('firebase-functions');
const moment = require('moment');

const timestamp = (req, res) => {
if(req.method!=='GET'){
return res.status(405).send(`${req.method} method not allowed`);
}

/** if a query parameter doesn't arrive in the request, use a default fallback */
const format = req.query.format || 'dddd, MMMM Do YYYY, h:mm:ss a';

return res.status(200).send(moment().format(format));
};

module.exports = {
api: functions.https.onRequest(timestamp),
};

Here, I have given the option for someone hitting our cloud function endpoint to give us a format in the query parameter.

For that, the request URL will look something like this:

1
curl https://.../api?format=do%20MMMM%20%20YYYY

Response:

1
4th May 2018

That’s all.