0

There are various ways of adding a day for example:

import datetime
print(datetime.datetime.now() + datetime.timedelta(days=1))

The problem with this is that the answer is: 2016-12-06 16:52:44.679431 I only need 2016-12-06. I can easily get that by performing a string manipulation like splitting. I as wondering if there was a way to do it directly.

secondly:

From what I have read from the documentation the below two ways should give me the time in my timezone neither do though.

import time
print(time.localtime())

result: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=5, tm_hour=17, tm_min=50, tm_sec=56, tm_wday=0, tm_yday=340, tm_isdst=0)

&

import datetime
print(datetime.datetime.now())

return 2016-12-05 17:52:09.045170

neither do, they both give me GMT:

How do I get my local timezone?

Summary: Is there a direct way to a day and get the correct form? an How do I get my local timezone?

Tank
  • 501
  • 3
  • 19

1 Answers1

1

According to the documentation:

datetime.date()

Return date object with same year, month and day.

In your case:

import datetime

print("local hour: "+str((datetime.datetime.now() + datetime.timedelta(days=1)).date()))
print("utc hour: "+str((datetime.datetime.utcnow() + datetime.timedelta(days=1)).date()))

Output:

local hour: 2016-12-06
utc hour: 2016-12-06

Another way is change datetime.datetime.today() to datetime.date.today():

import datetime

print(datetime.date.today() + datetime.timedelta(days=1))

Output:

2016-12-06
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Make sure to address all OP's questions: "Does datetime.now and datetime.today give you thet time in your time zone? Fro my previous code I am assuming it gives GMT so is there a direct way to change the timezone?" Heck, I'll just answer. The docs claim that that the time returned from `today()` and `now()` is local time. – Steven Rumbalski Dec 05 '16 at 17:28
  • the docs claim local time but I get GMT?? I read the docs before and that is why I am confused. Added examples to orginal q. – Tank Dec 05 '16 at 17:59
  • the day component in local hour and UTC will be the same until about 9PM but the website I am building will definitely have a bug if the date and time I am using is GMT. – – Tank Dec 05 '16 at 18:27
  • @Tanc27: Did the code that gave GMT run on your local computer or on the web server? – Steven Rumbalski Dec 05 '16 at 18:31
  • I'm using cloud 9 IDE. so I guess they use GMT. I should have realized sorry. Do I just subtract five hours or can I change the timezone in my IDE? – Tank Dec 05 '16 at 18:34
  • Perhaps see [How to convert a python utc datetime to a local datetime using only python standard library?](http://stackoverflow.com/questions/4563272/how-to-convert-a-python-utc-datetime-to-a-local-datetime-using-only-python-stand/13287083#13287083) – Steven Rumbalski Dec 05 '16 at 18:44