How to convert Python Datetime to String without microsecond component

0 votes
I want to create a string without a  microsecond component, from a datetime instance with a microsecond component.
Aug 2, 2019 in Python by Taj
• 1,080 points
2,173 views

1 answer to this question.

0 votes

If you want to format a datetime object in a specific format, it is better to explicitly specify the desired format. You can use the below given code:

Input:

import time
time.strftime("%Y-%m-%d %H:%M:%S")

Output:

'2019-08-02 11:42:11'

The above shown code used local time by default, if you need UTC you can use the following:

Input:

time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())

Output:

'2019-08-02 11:44:14'
answered Aug 2, 2019 by Shabnam
• 930 points

Related Questions In Python

0 votes
1 answer

How to convert date string to date object in python?

Use this :-  >>> datetime.datetime.strptime('24052010', "%d%m%Y").date() datetime.date(2010, 5, 24) Hope ...READ MORE

answered Oct 22, 2018 in Python by Priyaj
• 58,020 points
11,233 views
0 votes
1 answer

How to convert datetime.date to UTC timestamp in Python

In Python 3 (< 3.3) From the docs ...READ MORE

answered Oct 31, 2018 in Python by Priyaj
• 58,020 points
9,936 views
0 votes
1 answer

How to convert an integer to a string using Python?

Here is an easy solution: def numberToBase(n, b): ...READ MORE

answered Nov 23, 2018 in Python by Nymeria
• 3,560 points

edited Dec 12, 2018 by Nymeria 1,162 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+1 vote
1 answer

How to check if a string is in a file, using python?

Hi, Try the below given code: with open('myfile.txt') as ...READ MORE

answered Jul 5, 2019 in Python by Shabnam
• 930 points
2,030 views
0 votes
1 answer

How to add a new Python interpreter in PyCharm?

Refer to the below screenshots: Then set a ...READ MORE

answered May 30, 2019 in Python by Shabnam
• 930 points
5,427 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP