Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Website Domains Names & Hosting | Domain.com

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Instant Grammar Checker - Correct all grammar errors and enhance your writing

Liquid Web Cloud VPS Hosting

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

12,654 questions

17,243 answers

573 users

How to get the current date and time in Python

8 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
...