Convert unix time (or epoch) to formated date string

Hey there! It sounds like you're working with time conversions in your RTClib project. While the DateTime() function can be a helpful tool, converting Unix timestamps to formatted strings requires an extra step.

Here's a simple approach to achieve this:

  1. Obtain the Unix Timestamp: I assume you're already getting the Unix timestamp value using your RTClib setup (e.g., from the RTC itself).

  2. Create a DateTime Object: Use the unixtime() function on your DateTime object to create a new instance representing the Unix time:

  3. Format the Date and Time: Now you can use the DateTime object's formatting methods to create a human-readable string. Common options include:

  • now.timestamp(DATETIME_FORMAT) (replace DATETIME_FORMAT with your desired format string, e.g., "%Y-%m-%d %H:%M:%S")
  • Separate calls for individual components (e.g., .year() , .month() , .day() , etc.) for more control over the output.

Additional Resources:

For a comprehensive reference on DateTime object formatting and other time manipulation techniques in RTClib, check out the library's documentation.

This approach should help you convert Unix timestamps to formatted strings using RTClib's DateTime object. Feel free to ask if you have any further questions about time conversions or RTClib functionalities!

1 Like