formatting time in python

The program below converts a datetime object containing current date and time to different string formats.

Code:
  
from datetime import datetime

now = datetime.now() # current date and time

year = now.strftime("%Y")
print("year:", year)

month = now.strftime("%m")
print("month:", month)

day = now.strftime("%d")
print("day:", day)

time = now.strftime("%H:%M:%S")
print("time:", time)

date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time)	

Output after run the code:
year: 2020
month: 03
day: 31
time: 04:59:31
date and time: 03/31/2020, 04:59:31
      
Here, year, day, time and date_time are strings, whereas now is a datetime object.

0
8
John Hays 100 points

                                    Format time python

0
0
4
4
Miniml 110 points

                                    | Directive | Meaning                                                        | Example                 | 
|-----------|------------------------------------------------------------------------------------------|
|%a         | Abbreviated weekday name.                                      | Sun, Mon, ..            | 
|%A         | Full weekday name.                                             | Sunday, Monday, ...     | 
|%w         | Weekday as a decimal number.                                   | 0, 1, ..., 6            | 
|%d         | Day of the month as a zero-padded decimal.                     | 01, 02, ..., 31         | 
|%-d        | Day of the month as a decimal number.                          | 1, 2, ..., 30           | 
|%b         | Abbreviated month name.                                        | Jan, Feb, ..., Dec      | 
|%B         | Full month name.                                               | January, February, ...  | 
|%m         | Month as a zero-padded decimal number.                         | 01, 02, ..., 12         | 
|%-m        | Month as a decimal number.                                     | 1, 2, ..., 12           | 
|%y         | Year without century as a zero-padded decimal number.          | 00, 01, ..., 99         | 
|%-y        | Year without century as a decimal number.                      | 0, 1, ..., 99           | 
|%Y         | Year with century as a decimal number.                         | 2013, 2019 etc.         | 
|%H         | Hour (24-hour clock) as a zero-padded decimal number.          | 00, 01, ..., 23         | 
|%-H        | Hour (24-hour clock) as a decimal number.                      | 0, 1, ..., 23           | 
|%I         | Hour (12-hour clock) as a zero-padded decimal number.          | 01, 02, ..., 12         | 
|%-I        | Hour (12-hour clock) as a decimal number.                      | 1, 2, ... 12            | 
|%p         | Locale’s AM or PM.                                             | AM, PM                  | 
|%M         | Minute as a zero-padded decimal number.                        | 00, 01, ..., 59         | 
|%-M        | Minute as a decimal number.                                    | 0, 1, ..., 59           | 
|%S         | Second as a zero-padded decimal number.                        | 00, 01, ..., 59         | 
|%-S        | Second as a decimal number.                                    | 0, 1, ..., 59           | 
|%f         | Microsecond as a decimal number, zero-padded on the left.      | 000000 - 999999         | 
|%z         | UTC offset in the form +HHMM or -HHMM.                         |                         | 
|%Z         | Time zone name.                                                |                         | 
|%j         | Day of the year as a zero-padded decimal number.               | 001, 002, ..., 366      | 
|%-j        | Day of the year as a decimal number. 1, 2, ..., 366            |                         | 
|%U         | Week number of the year (Sunday as the first day of the week). | 00, 01, ..., 53         | 
|%W         | Week number of the year (Monday as the first day of the week). | 00, 01, ..., 53         | 
|%c         | Locale’s appropriate date and time representation.             | Mon Sep 30 07:06:05 2013|
|%x         | Locale’s appropriate date representation.                      | 09/30/13                | 
|%X         | Locale’s appropriate time representation.                      | 07:06:05                | 
|%%         | A literal '%' character.                                       | %                       | 

4 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
time.time() format in python python string format time python time.time formatting puthon string time format time:"TIME_FORMAT" python time:"time format" python python get time in format time format strings in python time from string format python time format for python time formates in python time format in pyhton string format python time python standard time format pythong time field format time python formats time format in python datetime formate time() python python time string format time formate py format time.time python time format string python oython format time python format time.time() to proper string python format time.time date time how to format python python format string time python date time format time standard format pythion date time in python standard format how to get time format in python python time code format format of time in python time formate in python datetime time format python python, strftime format time.time format python python str format time python datetime format with time time date format python time formats in python python3 format time time formats python format date time python time value format python how to format time in python formatting python datetime python datetime.time format format method python datetime python time formatting python time format reference how to write the formatted time in python time format - 1j python python to time format format time in python time python datetime format time format time date python formatting time in python datatime pyton to string format(time())) python format time in python python time date format python time library format python format date time python time formats standard time format python python time display format python str(datetime) format python time format string datetime strf - python convert python time to format time python python strf.time strfprint python time. python strftime format string hours minutes seconds formatting time python date data type python strftime %y-%m-%d strftime get day python date format tom string pyhton format python datetime python datetime object reformat python strftime codes python formate datetime python3 strftime strptime format python python date formats convert year to string python dt.now formatting strftime day python date formate date time format minutes python python datetime formats strftime year python python date type string python datetime format codes how to get a value from python strftime dateime to string format datetime string in python datetime.now().strftime python now.strftime datetime in python dtaetime format python python3 print datetime from string datetime year import datetime as dt python time format example how does deltatime work python format of datetime python python time of day python datetime strftime parameters date.strftime year python date time library strftime("%Y-%m-%d, %H:%M") timedelta days datetime formats python python datetime.strptime to string strf string print timestring day py datetime to string python built in time format html python timespan python strftime("%m") format date type python datetime table python make datetime to string python strftime mask python time module datetime ptyhon timedelta datetime.time to hours python delta time day number python datetime standard format of date python how to import datetime from datetime in python python timedelta days python non local time as timezone time datetime.datetime using timedelta python datetime formate what is the format of datetime.datetime python strftime format datetime.time to str integer input html time delta years months days minutes how format time in python how to format the time in python datetime.time python datetime functions strftime h:m:s python time get time formated python datetime.time sttrftime python time.strftime example from datetime import date, datetime new date in python datetime in pytohn date time format python python date to string format format date string python python time.time vs datetime.now python date time functions on website datetime datetime python day datetime python date formating what is datetime in python date module in python python for datetime take date components from python date object using date objects in pthon datetime python hour minute format datetime python month name code python format datet to string time.strftime("%-2I) create datetime.timedelta datetime type strftime formats in python strftime formatting python datetime.strftime change date to string python date formats python date format in python def today.strftime("%d/%m/%Y")strftime(self, fmt): datetime.datetime python to string time python strftime python datetime.datetime.strftime time formatting in python python datestring from date time python time.strftime python datetime strftime format options datetime strftime format python month type in python python datetime to str d b y date format python python datetime now strftime python datetime formating mounth name in python date format datetime days python datetime string formatting python format datetime to date python datetime format datetime python time object to string python strftime formats python datetime format strings date time formats in python datetime strftime python datetime formatting python python strftime formats dateime.todya().strftime("%b')) strftime format :2 list of date time formats python python format strtime python format datetime time srftime python pqthon time to string time module in python minutes python datetime string patterns python datetime as clasmethod struct_time to datetime python timedelta format time.strftime docs dtea to str python to datetime format python datetime to string py python time documentation .strftime("%H:%M:%S") code datetime to stftime convert datetime format to string python datetime now format python 3 from datetime to string python python time.time() at 6 pm python datetime days date format python strftime import datetime datetime python python format date to string pattern python time.time string format %I strftime {{ time.strftime("%Y-%m-%d") }} datetime.now.strftime python how to format datetime object python Python strftime() 2012.917 python datetime string strf parse time.time python format the time in python python strftiem time api python time string in python strftime date python now datetime as long string python ctime to datetime "'"+str((datetime.datetime.now()).strftime("%Y-%m-%d"))+"'" with hours and minutes strftime('%Y-%m-30) format datetime.now() python python strtime no strtime str now strftime datetime python time from string python convert gmtime to datetime strftime('%Y%m%d') time localtime python python: print datetime string isSecond in python time docs python tim b python time library documentation python strftime python formats python time format timw format time.time() python string to python datetime python add time format string how to use datetime strftime python with time "{time}".format(10) python datetime strftime format using strftime in python timedata.timedelta python date now to string python strftime to string import timedelta inpython ind atetime datetime python timedelta days timetostr python convert now to string python time.strptime python datetime object to string python parse period 1.2015 to date in python format python time python date object to string import timedelta in python python date time formats datetime.timedelta(days=1) python python time.time to hours datetime python string GMT TIME IN STRING PYTHON gmt data time string convert in data time python gmt data time dting convert in data time python time api pythin python datetime as string python string time representation python convert date time to string python string from datetime object pytohn string from datetime object today.strftime("%y-%m-%d") what date style? datetime encoding python datetime.datetime.now().strftime("") python datetime() new datetime python datetime.datetime.strftime python python datetime now format python from datetime to string timestamp to format python year and time with seconds format in python date from string python get strftime from dates in python time.strftime in python time localtime python example formats python datetime.date to str python format time.delta strftime in python+html python time and date format why is python cMIME lled pythob python datetime to korean format string introduce a date in python string time to string in python python datetime.datetime to string time methods python day of the week python localtime is not defined datetime strftime python formats today datetime python date time string python python date object string format python date and time string python strftime format list timedelta weeks python datatime module methods python python datetime time to string strftime python format codes print datetime as string python time.strptime python python delta datetime from 0 get string from datetime python strftime datetime format python datetime text python datetime date python python date strftime formats date to string in python data time format python python datetime format code python string datetime iso to utc python python datetime strftime format data time math python string format of date time on pythn format datetime python python time utc + 7 python datetime foramt python 3 datetime now format how to convert datetime object to string in python python datetime str python time time to datetime python date to string formatter python get datetime string datetime python format python datetime string format python datetime time from string python time object how to handle dates in python python timedelta hours example python time object with year month date get strftime from timestamp python Python strftime timestamp 24 hour time format in python python date methods python utcfromtimestamp timezone python string from time datetime object .date import datetime from datetime datetime.day python python strftime formatstring strftime examplew import datetime PANDAS datetime.now().strftime(" python date time python strftime('%Y-%d-%B') datetime datetime to string python timestamp format format date python python date objects strftime in python datetime module in python documentation python convert time day and time python python format ctime to date string import strftime python get format datetime python date object to string python python timestamp string python datetime strptime format list formating date time object datetime.strftime python python time strftime python time.timezone() datetime today python strftime datetime today python strftile python datetime import python time format 12 hour datetime time python timedelta python format date library python time time python arguments date to stringpyt python 3 datetime to string datetime.date to string datetime strptime format python date time in python strftime example python python format for datetime now.strftime in python python get time tuple datetime.datetime to string python python time standard format python datetime.date to string python format datetime string get date string from datetime python date time to string in python format datetime.datetime as string python buit in string time python time from string format python + day python - day how to format a time in datetime python python3 get datetime as string datetime date format python time 12 hours get time in different format python python get date as string python get date to string date in python 12 hour python format python datetime timedelta python how to store datatime object as a string python timestamp to string python datetiem strftime python example datetime.date in python datetime.datetime.now() to string python python datetime date as string exemple strptimpe python strftime timedelta python get datetime time to string python3 convert date to string date format table python python dateformat string datetime.date string format python timedelta.days datetime.strftime python 3 convert time to string python time.time() format python pythondatetime to string timestamp to string python python from timestamp to string formatting datetime python python from date to string how to convert a date into a string in pyton python time.time() delta.timedelta python python datetime delta convert time to string in python strftime python format python format datetime datetime.datetime to string python format datetime to string date python datetime.strptime python strftime example python create gmt time object python string format datetime datetime.hour python datetime to str change to strftime datetime.datetime.now() in python format strftime python timerstamp to string python time to string python datetime timedelta import timedelta python datetiem to str date format python python timedelta python date strftime python code to get date as string datetime.now() to string datetimr.strftime strings datetime python python type time time in python timedelta python datetime.datetime python datetime.now to string .strftime python datetime.now format python python date format list python date format string dates in python time.strftime datetime formatting in python python time to string datetime striptime python timedelta strftime("%d/%m/%y") datetime strftime python strformat datetime.month python turn datetime to string python how to get localtime in strptime python get datetime to string python date time to date str python python datetime to date string myString = myDatetime.strftime('%Y-%m-%d %H:%M:%S') python3 date to string date format in python datetime fromatted time with time python .strftime now.strftime python python str from time python gmtime import date into python python time strftime %X format date to string python python format date to hour tadetime python python2 datetime.datetime datetime now string format python datetime into string python time format in python datetime strings python datetime to string format python python datetime month datetime.now python to string d.time() python python day of week string strftime datetime python timestamp format in python python stftime format datetime to string date time pythopn datetime python datetime.now.strftime codes python datetime string format codes datetime.now.strftime python time date datetime format python datetime.datetime python datetime python month python timedelta keywords python datetime datetime python to string date to string python python time how to format tell time with python time python python time library python strftime format {time} python format time.format python python datetime string what is strftime python python strf datetime python datetime to formatted string python time to string format strftime() python strftime function in python dattime get formatted time python python format date string python format time python format datetime as string python date format time python modual Moday output python datetime strftime datetime python format string format time python python datestring datetime string format python time format python python strftime python time format python time timezone python datetime date format python datetime format datetime as string python python datetime now to string python datetime date format string datetime python format strftime example strftime python strftime("%-m/%-d/%Y") python time.strftime(' d/ m/ y') python datetime to string python python date to string time class python how to use strftime in python python datetime.now stringformat python datetime is a string datetime format list python python datetime format string date strings python datetime.date to string python strftime python python datetime to string date string python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source