Datetime returns wrong time in unix

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Datetime returns wrong time in unix



I have a wired situation here with returning the time in unix format.



I have this function:


def get_today_date():
nowDate = str(datetime.datetime.utcnow().date())
unix_date = time.mktime(datetime.datetime.strptime(nowDate, "%Y-%m-%d").timetuple())
return (unix_date)



it's straightforward when I print the value of "nowDate" -> it gives me the right value I want (ex: 7/29/2018 00:00:00 UTC) which is correct. But when I change the format from Unix timestamp to date format and get only the date without time, It gives me (7/28/2018).



I've took the value in unix and checked it, it gives me (7/28/2018 21:00:00). Why?



My laptop is in a UTC +3 timezone.



Did something go wrong in the conversion? Or its an internal error in my laptop makes the output wrong?





Some of the data time functions take timezone into account and some don't. This is where you probably made a mistake, but it is not clear what exactly you did, because you say that nowDate is '7/29/2018 00:00:00 UTC', but that cannot be, because it is a date string, e.g. '2018-07-29'. You also say that unix_date is 7/28/2018 21:00:00, but that also cannot be, because it is just a number, e.g. 1532815200.0. So, please show the exact code you run which gives you the wrong result, so we can tell you exactly what you did wrong.
– zvone
7 mins ago




nowDate


'7/29/2018 00:00:00 UTC'


'2018-07-29'


unix_date


7/28/2018 21:00:00


1532815200.0





BTW, to get today's date, you only need to call datetime.date.today().
– zvone
7 mins ago


datetime.date.today()




1 Answer
1



If you're looking for today's date/time, use datetime.datetime.now(), if you need just the date, use datetime.datetime.today() it's automatically converted into your timezone.


datetime.datetime.now()


datetime.datetime.today()



If you play with UTC-based functions, you'll get 3 hours offset for your location, which sometimes might result in time being 21:00 of previous day instead of midnight of the next one -- that's quite reasonable =)


UTC






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Better method to check all objects' parameters with a single call

Visual Studio Code: How to configure includePath for better IntelliSense results