Am I the only person on the planet that wants to convert a datetime to string or char array?
I have searched for examples.
I want to convert the current datetime to a string or char array.
string dt = DateTime().tostring();
I need a string like this:
yyyy.mm.dd.hh.mm.ss
2014.10.22.17.25.30
With our without the periods.
Can someone help on this simple request?
Thank you.
https://www.google.com/search?q=localTime+sprintf+site%3Aforum.arduino.cc
Second hit...
Change to suit...
////////////////////////////////////////////////////////
//
// PrintTime
//
// Serial Print time (DD/MM/YYYY - HH:MM:SS)
//
void PrintTime() {
char cTime[22];
sprintf(cTime, "%02u/%02u/%4u - %02u:%02u:%02u", day(), month(), year(), hour(), minute(), second());
Serial.println(cTime);
}
Thank you so much for your reply. I can move on in my project.
Coding B.
i am a c# programmer and am just staring out with Arduino. I wonder if you could help me with this next step.
I need to incorporate the datetime string that you helped me with into my json object that I am sending up to Azure.
I have the time and need to assign it to the activitytime value, but i am mixing a char with a string and that is not allowed.
char cTime[22];
sprintf(cTime, "%04s/%02s/%2s:%02s:%02s:%02s", year(), month(), day(), hour(), minute(), second());
sprintf(buffer, "{"location":"26 Oak", "activitytime":"" + cTime + ""}");
Thank you.
char cTime[22];
sprintf(cTime, "%04s/%02s/%2s:%02s:%02s:%02s", year(), month(), day(), hour(), minute(), second());
sprintf(buffer, "{\"location\":\"26 Oak\", \"activitytime\":\"%s\"}", cTime);
Or...
sprintf(
buffer,
"{\"location\":\"26 Oak\", \"activitytime\":\"%04s/%02s/%2s:%02s:%02s:%02s\"}",
year(), month(), day(), hour(), minute(), second() );
Thank you very much for your help.
Terrence
Coding B, I have been messing with this time conversion all week. I figured I could figure it out with your previous help, but I am stuck.
The code you posted causes an error and I have no idea how to debug it.
Here is the error.
B0100000063f694
this happens on this line of the sketch: sprintf(cTime, "%0
Here is the sketch
#include <Time.h>
char buffer[64];
void setup() {
Serial.begin(9600);
char cTime[22];
sprintf(cTime, “%04s/%02s/%2s:%02s:%02s:%02s”, year(), month(), day(), hour(), minute(), second());
sprintf(buffer, “{“location”:“26 Oak”, “activitytime”:”%s"}", cTime);
}
void loop(){
}
I figured it out. It's d not s
sprintf(cTime, "%04d.%02d.%02d.%02d:%02d:%02d", year(), month(), day(), hour(), minute(), second());
Thanks for your help.
Terrence