In python how can I get the date and time of my local server?

6.1k views Asked by At

I am aware of the datetime library. However, on top of knowing the date and time of the world, I also need to know the date and time of the local server my application will be running on. The reason for this is because my local server is not connected to the internet so it may have a different date and time and I want to be able to detect it if that's the case. Any help would be appreciated.

2

There are 2 answers

1
jmd_dk On BEST ANSWER

With the datetime package, as you refer to, you can get the current local time e.g. by

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

This is the local time on the machine. Python does not reach out to the world over the internet to ask what time it is.

0
Frank On

There is also the time module which has a localtime method. However this just reformats time.

import time
localTime = time.localtime()
print (localTime)