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,040 points
144 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
• 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
• 57,490 points
629 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
• 57,490 points
3,686 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 65 views
0 votes
1 answer

How can I write code to find a palindrome in python without using string functions?

In Logic 1, try if i<int(length/2): instead of if i<int((length/2+1)): In ...READ MORE

answered Jan 16, 2019 in Python by charlie_brown
• 7,720 points
4,637 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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 539 views
0 votes
0 answers
+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, 2019 in Python by Shabnam
• 920 points
79 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
• 920 points
952 views