Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,961 questions

51,903 answers

573 users

How to get the current date and time in Python

11 Answers

0 votes
from time import gmtime, strftime

t = strftime("%Y-%m-%d %H:%M:%S", gmtime())

print(t)


'''
run:

2017-11-06 07:12:08

'''

 



answered Nov 6, 2017 by avibootz
edited Nov 6, 2017 by avibootz
0 votes
from datetime import datetime

t = str(datetime.now())

print(t)


'''
run:

2017-11-06 09:31:35.054568

'''

 



answered Nov 6, 2017 by avibootz
0 votes
from datetime import datetime

t = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

print(t)


'''
run:

2017-11-06 09:32:30

'''

 



answered Nov 6, 2017 by avibootz
0 votes
import datetime

t = datetime.datetime.now()

print(t)


'''
run:

2017-11-06 09:33:25.130864

'''

 



answered Nov 6, 2017 by avibootz
0 votes
import time

t = time.ctime()

print(t)


'''
run:

Mon Nov  6 09:52:08 2017

'''

 



answered Nov 6, 2017 by avibootz
0 votes
import time

print(time.ctime(time.time()))
      
         
         
'''
run:

Sun Jun  2 17:37:04 2019
            
'''

 



answered Jun 2, 2019 by avibootz
0 votes
import datetime

print('now:', datetime.datetime.now())
print('today:', datetime.datetime.today())     
print('utcnow:', datetime.datetime.utcnow())

'''
run:

now    : 2019-06-06 13:49:26.393668
today  : 2019-06-06 13:49:26.393765
utcnow: 2019-06-06 13:49:26.393786

'''

 



answered Jun 6, 2019 by avibootz
0 votes
import datetime

dt = datetime.datetime.now()     

FIELDS = ['year', 'month', 'day',         
          'hour', 'minute', 'second',
          'microsecond',]

for f in FIELDS:         
    print('{:11}: {}'.format(f, getattr(dt, f)))



'''
run:

year       : 2019
month      : 6
day        : 6
hour       : 13
minute     : 55
second     : 40
microsecond: 256390

'''

 



answered Jun 6, 2019 by avibootz
0 votes
import time
 
print(time.strftime("%d/%m/%Y"))

print(time.strftime("%H:%M:%S"))



'''
run:
 
22/06/2015
16:11:17
 
'''

 



answered Apr 15, 2024 by avibootz
0 votes
import time
 
print(time.strftime("%c"))



'''
run:
 
06/23/15 11:32:29
 
'''

 



answered Apr 15, 2024 by avibootz
0 votes
import time
 
print("date: " + time.strftime("%x"))
print("time: " + time.strftime("%X"))
 

'''
run:
 
date: 06/23/15
time: 19:12:09
 
'''

 



answered Apr 15, 2024 by avibootz

Related questions

1 answer 176 views
3 answers 215 views
1 answer 280 views
1 answer 137 views
1 answer 222 views
1 answer 161 views
...