Change date and time format in Python.

0 votes

Hi. I am new to Python and I am writing a script. What I am trying to do is, get the current time and store it so that I can use it in a logic somewhere else. I am using the following code to get the current time.

import datatime 

time= datetime.datetime.now()
print(time)

This script is returning the time and date in the below format:

2019-05-08 16:29:14.960470

I want to store time and date separately in two different variables: time and date.

How to do this?

May 8 in Python by Sparsh
41 views

1 answer to this question.

0 votes

Try the following code:

date=time.split(‘ ’)

time=date[1].split(‘.’)

date=date[0]

time=time[0]

print(date, time)
answered May 8 by Sagar

Related Questions In Python

0 votes
1 answer

Is multi-threading supported in Python and can it speed up execution time as well?

The GIL does not prevent threading. All ...READ MORE

answered Nov 22, 2018 in Python by Nymeria
• 3,520 points
157 views
0 votes
1 answer

How to parse date/time string with timezone abbreviated name in Python?

The parse() function in dateutil can't handle ...READ MORE

answered Nov 27, 2018 in Python by Nymeria
• 3,520 points
172 views
0 votes
1 answer

How to check latest change time of each file using glob in python?

You can get the changing time from ...READ MORE

answered Jan 2 in Python by Omkar
• 67,380 points
61 views
0 votes
0 answers

How can you print the date in a regular format in python?

Is there a module or package in ...READ MORE

Jun 18 in Python by Waseem
• 4,480 points
12 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 221 views
+4 votes
6 answers
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 56,520 points
213 views
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
79 views