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 in Python by Taj
• 1,040 points
29 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 by Shabnam
• 920 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 ...READ MORE

answered Oct 22, 2018 in Python by Priyaj
• 56,520 points
134 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
• 56,520 points
1,907 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,520 points

edited Dec 12, 2018 by Nymeria 42 views
0 votes
1 answer
+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 224 views
+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 4 in Python by Shabnam
• 920 points
39 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 in Python by Shabnam
• 920 points
440 views