conversion DateTime to unixtime

Hi, i'm trying to convert DateTime to unix time, using RTC ds3231. But the unixtime() method doesn't work in my program.

#include "RTClib.h"
#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
RTC_DS3231 rtc;
DateTime dt ;
time_t timenow ;

void setup () 
{
  Serial.begin(9600);
  Wire.begin();
  dt = rtc.now();
  timenow = dt.unixtime();
 
}
 void loop () 
{ 
  dt = rtc.now();
  Serial.println(dt.month());
  timenow = dt.unixtime();
  Serial.println(month(timenow));
  delay(1000);
}

For example i tried to compare both of the month's results
the first Serial command give me 7, the current month. but the second gave me another variable result
can anyone help me please?

.unixtime() does not return a time_t but a uint32_t.

thank you. but i changed the 'timenow' variable to uint32_t and the results are still difference...

As far as I can see month does expect a time_t. Aka, you try to do something that's not possible the way you do it. I don't see an easy way of moving back from unix time. Any reason why you want to use unix time?

i need it for future calculations. for example, some certain dates in next year. I thought the easiest way to deal with it is with unix time.

I think a time_t variable is fine for that as well. Although it's been a while since I used it...

that's too complicated to change date. i don't know how many second to add for a month or for a year, because it's not a constant value.

That's why it comes with all the neat functions... You can use those to add/subtract time. So don't really see the problem there. Especially because you have the same problem with a unix timestamp.

I am curious as to what exactly dt.now is returning. I would expect a function that returns a time_t to be Epoch time. (Unix Time). So what exactly is being return by dt.now?

OP, for my own curiosity, can you output the 2 values. the dt.now as well as the dt.UnixTime.
You should be able to use this site.

To convert the value. I have never used an RTC on an arduino, might have to pull one out of the bin.

the first Serial command give me 7, the current month. but the second gave me another variable result
can anyone help me please?

I can not confirm your issue. I load your code onto a nano with 1.8.5 using a DS3231 and I see 7 printed each time.

cattledog - i don't know what to say. i get difference result.

Romonaga - i'm not sure i understood you, but i get the same result for:
Serial.println(dt.unixtime());
Serial.println(timenow);
very strange for me...

yossi84:
cattledog - i don't know what to say. i get difference result.

Romonaga - i'm not sure i understood you, but i get the same result for:
Serial.println(dt.unixtime());
Serial.println(timenow);
very strange for me...

Can you share the values you see?

2313943001
2313943001
something is wrong with my system. i don't know what happend meanwhile, but some hours ago i got the right value in both of commands.