Been struggling with something for a few days.

Loading data from a website via JSON and it includes an updated date time in milliseconds. This is an EPOCH date and I can get the right value using some of the online convertor sites, epochconvertor.com

That said when I try and parse it in my sketch it’s not retuning the correct date.

JSON “currentupdatedms”:1588312648603

Convertor Shows as: 1 May 2020 05:57:28.603

My sketch is showing: 1 May 2020 02:33:00

That’s in the JSON and I can’t use a Long as it’s 13 digits. Tried Long Long and it accepts the value but when I later divide by 1000 to get seconds the number changes completely when I try printing it to serial or the OLED.

Plan is to use the Timeh library to display it as a human date time.

Any thoughts be appreciated.

Here is my code and I have tried various methods:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <TimeLib.h>
#include <time.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

#define ARDUINOJSON_JSON_LONG_LONG 1 //Not needed???
#define   ONE_HOUR   3600
const size_t capacity = JSON_OBJECT_SIZE(8) + 140;
DynamicJsonDocument doc(capacity);

   Need to look at conversion of Update and Changed times as they are not showing correctly
   Move the conversion of time datetime in ms to a seperate function
   Move the get request and JSON section to seperate function

const char* ssid = "SSID";
const char* password = "PWD";

void setup () {


  Wire.begin(2, 0);           // set I2C pins (SDA = GPIO2, SCL = GPIO0), default clock is 100kHz

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever


  WiFi.begin(ssid, password);

  display.setCursor(0, 0);
  display.println(F("Connecting to Wifi..."));

  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to Wifi...");

  display.setCursor(0, 10);
  display.println(F("Wifi Connected..."));

void loop() {

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    display.setCursor(0, 20);
    display.println(F("Requesting Data..."));
    delay(2000); // Pause for 2 seconds

    HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("http://corona.tuply.co.za/DataHandler.ashx?w=s&uq=8f1fp8p5jdxi84481");  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request

    if (httpCode > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload

      deserializeJson(doc, payload);

      int TotalCases = doc["TotalCases"];
      int TotalCasesToday = doc["TotalCasesToday"];
      int TotalDeaths = doc["TotalDeaths"];
      int TotalDeathsToday = doc["TotalDeathsToday"];
      int TotalRecovered = doc["TotalRecovered"];
      int TotalTerritories = doc["TotalTerritories"];
      long long CurrentUpdatedMS = doc["CurrentUpdatedMS"];
      long long CurrentChangedMS = doc["CurrentChangedMS"];


      time_t CurrentUpdateMS_t = (CurrentUpdatedMS/1000) + ONE_HOUR; //ONE_HOUR added for BST

      char buffUpdateMS[32];
      sprintf(buffUpdateMS, "%02d/%02d/%02d %02d:%02d:%02d", day(CurrentUpdateMS_t), month(CurrentUpdateMS_t), year(CurrentUpdateMS_t), hour(CurrentUpdateMS_t), minute(CurrentUpdateMS_t), second(CurrentUpdateMS_t));
      Serial.print("ctime: ");

      time_t CurrentChangedMS_t = CurrentChangedMS/1000 + ONE_HOUR; //ONE_HOUR added for BST

      char buffChangedMS[32];
      sprintf(buffChangedMS, "%02d/%02d/%02d %02d:%02d:%02d", day(CurrentChangedMS_t), month(CurrentChangedMS_t), year(CurrentChangedMS_t), hour(CurrentChangedMS_t), minute(CurrentChangedMS_t), second(CurrentChangedMS_t));
      Serial.print("ctime: ");


      display.setCursor(0, 0);
      display.print(F("T Cases: "));

      Serial.print("Total Cases: ");

      Serial.print("Total Cases Today: ");

      display.setCursor(0, 10);
      display.print(F("T Deaths: "));

      Serial.print("Total Deaths: ");

      Serial.print("Total Deaths Today: ");

      display.setCursor(0, 20);
      display.print(F("T Recov: "));

      Serial.print("Total Recovered: ");

      Serial.print("Total Territories: ");

      display.setCursor(0, 30);
      display.print(F("U T: "));

      display.setCursor(0, 40);
      display.print(F("C T: "));



    http.end();   //Close connection


  delay(60000);    //Send a request every 60 seconds


If I run that now I get this JSON string:


CurrentUpdateMS = 1589027095790 which is Sat May 09 2020 13:24:55, granted there may be an hour difference with BST.

Instead I am getting:

Sat May 9 13:23:32 2020

Also appears to stay the same after refreshes which I have set to 60 seconds.

I tried the Time.h library with same results.

Thankful for any input or suggestions.

  • 1. Do not mix time.h with TimeLib.h: they both define their own version of time_t. 2. This program is too long for a question here. You should break down your problem into small pieces and tackle them one at a time. First, Serial.println(CurrentUpdatedMS) in order to know whether the issue lies in the JSON deserialization or in the interpretation of the number as a time. Then, write a minimal, reproducible example addressing only that issue. See my answer as an example of what a “minimal, reproducible example” may look like. May 9, 2020 at 13:08

1 Answer 1


It's hard to know what is wrong with your code... because you are not showing it in the question! Here is a basic test I tried on my Uno:

#include <time.h>

const uint64_t ms = 1588312648603;

void setup() {
    time_t t = ms/1000 - UNIX_OFFSET;

void loop(){}

It's output is:

Fri May 01 05:57:28 2020

Note that this <time.h> comes from the avr-libc. It uses an epoch different from the Unix epoch, hence the UNIX_OFFSET correction.

Edit: I tried replacing avr-libc's <time.h> by Adafruit RTClib (declaring DateTime t(ms/1000);) and Paul Stoffregen’s Time library (with time_t t = ms/1000;). In all cases I get the same time displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.