NTPClient
NTPClient copied to clipboard
NTPC: how to obtain "getHours" in 24 hrs format?
Please let me know if is it possible to get NTC timeClient.getHours() in 24hrs format? Always getting 12 hr format.
My piece of Arduino code: Updating RTC chip with NTF time, if current time is 11PM then i got "11" but need "23": rtc.adjust(DateTime(YEAR, MONTH, DAY, timeClient.getHours(), timeClient.getMinutes(), SE));
BTW I know there is a way to use different approach e.g. epoch time or parse getFormattedTime etc...
try:
String getHours24(){ unsigned long hours = timeClient.getHours(); String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
return String( hoursStr ); }
@xkosm14 use dedicated direct-call functions or use code :-
String getHours24(){ unsigned long hours = timeClient.getHours(); String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
return String( hoursStr ); }
In NTPClient.cpp the following snippet-
int NTPClient::getHours() const {
return ((this->getEpochTime() % 86400L) / 3600);
}
Should ideally be able to return 23 as well.
Eg: for EPOCH = 1585436874 the return value should be 23.
Any clue on this? Are we talking about some other getHour()
function?