BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

[SOLVED] WidgetRTC Conv to > 12Hour AM PM Time Format?


#1

All,

I’ve churned through the forum for an answer to what I’m sure is a simple solutions, but I can’t find it. I’ve come close with @JoeS but can’t make it work .

Using WidgetRTC and the default code provided by @vhymanskyy how do you convert military time into a standard 12hr format with the AM, PM displayed??? Do I use hour12() inplace of hour() ? If so, how do you get the AM PM, isAM() or isPM()? Are these just True/False statements?

THANKS IN ADVANCE !

Cheers,

#include "BlynkSimpleEsp8266.h"
#include "ESP8266WiFi.h"
#include "ArduinoOTA.h"
//#include "DNSServer.h"
#include "SimpleTimer.h"
#include "Wire.h"
#include "WidgetRTC.h"
#include "WidgetLCD.h"
#include "WidgetLED.h"
//#include "LM75A.h"  // for a temperature sensor

#define BLYNK_PRINT Serial
#define SERIAL_EN
#ifdef  SERIAL_EN
#define SERIAL_BAUD   115200
#define DEBUG(input)   {Serial.print(input);}
#define DEBUGln(input) {Serial.println(input);}
#define SERIALFLUSH()  {Serial.flush();}
#else
#define DEBUG(input);
#define DEBUGln(input);
#define SERIALFLUSH();
#endif

char auth[] = "##################";
char ssid[] = "FBI Surveillance Van";
char pass[] = "2022782000";
char Date [16];
char Time [16];

SimpleTimer timer;
WidgetRTC rtc;

void clockDisplay()
{
  sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  sprintf(Date, "%02d/%02d/%04d",  month(), day(), year());
  Serial.print("\tCurrent time: ");
  Serial.print(Time);
  Serial.print("\t");
  Serial.print(Date);
  Serial.println();

  Blynk.virtualWrite(V1, Time);
  Blynk.virtualWrite(V2, Date);
}

void setup() {

#ifdef SERIAL_EN
  Serial.begin(SERIAL_BAUD);
#endif

  WiFi.mode(WIFI_STA);
  Blynk.begin(auth, ssid, pass);
  while (Blynk.connect() == false);
  delay(4000);
  yield();

  rtc.begin();
  delay(300);
  yield();
  
  setSyncInterval(300000); // Time Sync Every 5 min

  ArduinoOTA.setHostname("NewBuild"); // OPTIONAL
  delay(2000);
  yield();

  Serial.println("\t\tSet OTA Hostname New_Build ");

  Serial.println("");
  Serial.println("");
  delay(10);
  yield();
  Serial.println(WiFi.status());
  Serial.println("");
  Serial.println("");
  Serial.print("\tAssigned Local IP:\t\t");
  Serial.println(WiFi.localIP());
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\tWiFi  C O N N E C T E D !!!.");
    Serial.println("");
    Serial.println("");
    Serial.println("\tWifi Started");
  }

  ArduinoOTA.begin();
  delay(300);
  yield();

  Serial.println("\t\tOTA Began");
  Serial.println("");

  timer.setInterval(15000L, clockDisplay);

  //Serial.print("\t Blynk Ver: ");
  Serial.println(F("\tBlynk v" BLYNK_VERSION ": Device started"));
  Serial.println("");
}

void loop() {
  Blynk.run();
  timer.run();
  ArduinoOTA.handle();
}

#2

This is really more a Google question, solved by math (i.e you can code the difference in your sketch)


#3

Ya, @Gunner, I get the math! Thanks for pointing it out. At the end of the day this whole forum could be replaced with Google searches. Just to be fair I followed your link provided. It was a useless link. If you don’t know the answer to a simple question to help out other Blinkers on a topic under the heading "Need Help With My Project", maybe you should just pass. Again… maybe someday we’ll all know everything about Blynk, C++. Until that day comes, I’d still appreciate constructive advice.

Cheers,


#4

You are closer to the mark then you think on that one :slight_smile: This is a Blynk forum, but many of the questions ARE NOT Blynk related… that does muddy up the waters for those who have Blynk specific issues.


#5

WidgetRTC = Blynk

I’m going to stick with, it’s a valid question. I’m sorry if these inquiries are beneath some people.


#6

Even with Blynk, it still comes down to some form of code to manipulate the data to ones own preferences.

WidgetRTC = Blynk = Data generated != not the way you want it = Google for math solution to code differently = RESULTS!!! Yay! :smiley:

Insulting others in forum = being ignored or banned


#7

I couldn’t agree with you more.


#8

@Lane0138, the Time library has a documentation page which covers 12 hr convertions.

FYI, Time lib is not Blynk related and Google found this link :slight_smile:


#9

@Jamin, I’ll go back and review those pages again. I’ve looked through them at length prior to this post and didn’t understand the integration into the code.

Thanks


#10

I’ll give you a hand since its super stormy this weekend and its totally a computer day…

void clockDisplay()
{
  sprintf(Time, "%02d:%02d:%02d", hourFormat12(), minute(), second());
  sprintf(Date, "%02d/%02d/%04d",  month(), day(), year());
  Serial.print("\tCurrent time: ");
  Serial.print(Time);
  Serial.print("\t");
  Serial.print(Date);
  Serial.println();

  if(isAM()){
    Time += String(" AM");
  } else {
    Time += String(" PM");
  }

  Blynk.virtualWrite(V1, String(Time));
  Blynk.virtualWrite(V2, Date);
}

Give that a go…


#11

Thank You !!! @Jamin

I saw that in the Time Library but I couldn’t ever get the “hourFormat12()” to work. Rookie Syntax !

I hadn’t even got to the AM PM part yet.

Thank You Again… for lending me your knowledge.

Sorry about the weather !! :slight_smile:

Lane0138


#12

@Jamin,

I dread this post :confused: but… it won’t compile.

I’m getting:
49: error: no match for ‘operator+=’ (operand types are ‘char [20]’ and ‘String’)
Time += String(" PM");

Thanks for the Tip’s !

Lane0138


#13

Good example @Jamin Thank Heavens for bad beach days (once in awhile ;))

Syntax beats my hacked math method… I think it messes up on 0:00 hours… but then I wasn’t really doing any research as I prefer the military time (keeps me thinking) and I didn’t feel like being a code monkey today :stuck_out_tongue_winking_eye:

void clockDisplay()
{
  int HOUR = hour();
  if ( HOUR > 12 ) {
    HOUR = HOUR - 12;
    String currentTime = String(HOUR) + ":" + minute() + ":" + second() + " PM";
    Blynk.virtualWrite(V12, currentTime);  // Send PM time to the App
  } else {
    String currentTime = String(hour()) + ":" + minute() + ":" + second() + " AM"; ;
    Blynk.virtualWrite(V12, currentTime);  // Send AM time to the App
  }
  String currentDate = String(month()) + " " + day() + " " + year();
  Blynk.virtualWrite(V13, currentDate);  // Send date to the App
}

#14

Thank you both @Gunner and @Jamin for your coding assistance.

I couldn’t get the code to work right, so I asked for help.

Not my intention to turn someone into a “Code Monkey”.

Since I’m not a Pro-Coder and don’t understand A-Z, I’m going to guess that’s not a flattering comment.

Thank you for your help non the less.

Lane0138


#15

Just change your define for Time.

String Time; // instead of char Time

On a side note, avoid char variables… they just suck to convert lol


#16

You are speaking to a total amateur coder here as well… and while I do appreciate examples to glean from, I still rely HEAVILY on Google and https://www.arduino.cc/en/Reference/HomePage and try to only ask Blynk specific questions now (except when I am begging for Linux help… then I whimper like a baby ;P)

All is well… If I didn’t really care to help, even in a “Tough Love” format (as one other put it), I would NEVER answer anyone’s postings :wink:


#17

Great tip!!!

Thank You - - - Again


#18
  int HOUR;
  char APM[4]="";


void Timedate(){

  if ( hour() > 12 ) {
    HOUR = hour() - 12;
    strcpy(APM,"PM");

    } else {
    HOUR = hour();
    strcpy(APM,"AM");
    }
    
    String currentTime = String(HOUR) + ":" + minute() +":" + second() + " " + APM + "  " + month() + "/" + day() + "/" + year();
    terminal.println(currentTime);
    terminal.flush();
  }

#19