GameSparks Cloud Code - How to get current backend time (preferably time since epoch)

0

Hi,

I want to get current time as milliseconds to store in DynamoDB, along with some player data acquired from the client. So far create a new Event and use some of the below approaches:

const timeInEpoch = ${now};

const timeInEpoch = Date.now();

const timeInEpoch = new Date().getTime();

But it seems to me that none of the above code works, when I try the event with Test Harness, it return error result as: Enter image description here

Is there a way to retrieve this information? Thanks a lot!

Niles
asked a year ago217 views
1 Answer
1
Accepted Answer

Hi Niles!

Here's how you can get the current epoch, in seconds and milliseconds:

const epoch = new Date('1970-01-01T00:00:00Z');
const currentDate = new Date();

const timestamp_ms = currentDate - epoch;
const timestamp = Math.floor(timestamp_ms / 1000);

GameSparks().Logging().Info("Current timestamp, in seconds: " + timestamp);
AWS
Joris_D
answered a year ago
  • Thanks a lot, Joris, It worked now!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions