Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Presenting a Date-Time

To format human-readable time, we have a number of functions in the time module, and methods of a datetime object. Here are the functions in the time module.

time.strftime ( format , struct ) → string

Convert a struct_time to a string according to a format specification. The specification rules are provided below.

This is an example of how to produce a timestamp with the fewest implicit assumptions.

time.strftime( "%x %X", time.localtime( time.time() ) )

This line of code shows a standardized and portable way to produce a time stamp. The time.time function produces the current time in UTC (Coordinated Universal Time). Time is represented as a floating point number of seconds after an epoch.

time.asctime ( struct ) → string

Convert a struct_time to a string, e.g. 'Sat Jun 06 16:26:11 1998'. This is the same as a the format string "%a %b %d %H:%M:%S %Y".

time.ctime ( seconds ) → string

Convert a time in seconds since the Epoch to a string in local time. This is equivalent to asctime(localtime(seconds)).

A datetime object has the following methods for producting formatted output. In the following definitions, dt is a datetime object.

dt. isoformat( separator ) → string

Return string representing this date in ISO 8601 standard format. The separator string is used between the date and the time.

dt. ctime → string

Return string representing this date and time. This is equivalent to time.ctime(time.mktime( dt .timetuple())).

dt. strftime( format ) → string

Return string representing this date and time, formatted using the given format string. This is equivalent to time.strftime( format , time.mktime( dt. timetuple() ) ).

The strftime and strptime functions use the following formatting symbols to convert between 9-tuples and strings. Formatting symbols like %c, %x and %X produce standard formats for whole date-time stamps, dates or time. Other symbols format parts of the date or time value. The following examples show a particular date (Satuday, August 4th) formatted with each of the formatting strings.

%a Locale's 3-letter abbreviated weekday name. "Sat"
%A Locale's full weekday name. "Saturday"
%b Locale's 3-letter abbreviated month name. "Aug"
%B Locale's full month name. "August"
%c Locale's appropriate full date and time representation. "Saturday August 04 17:11:20 2001"
%d Day of the month as a 2-digit decimal number. "04"
%H Hour (24-hour clock) as a 2-digit decimal number. "17"
%I Hour (12-hour clock) as a 2-digit decimal number. "05"
%j Day of the year as a 3-digit decimal number. "216"
%m Month as a 2-digit decimal number. "08"
%M Minute as a 2-digit decimal number. "11"
%p Locale's equivalent of either AM or PM. "pm"
%S Second as a 2-digit decimal number. "20"
%U Week number of the year (Sunday as the first day of the week) as a 2-digit decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. "30"
%w Weekday as a decimal number, 0 = Sunday. "6"
%W Week number of the year (Monday as the first day of the week) as a 2-digit decimal number. All days in a new year preceding the first Monday are considered to be in week 0. "31"
%x Locale's appropriate date representation. "Saturday August 04 2001"
%X Locale's appropriate time representation. "17:11:20"
%y Year without century as a 2-digit decimal number. "01"
%Y Year with century as a decimal number. "2001"
%Z Time zone name (or '' if no time zone exists). ""
%% A literal '%' character. "%"

 
 
  Published under the terms of the Open Publication License Design by Interspire