Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Python Time strptime() Method with example



Hello guys! Welcome back to another section of my tutorial on Python. In this tutorial guide, we are going to be discussing about the Python's Time strptime() method.

The Python Time strptime() method parses a string that represents a time according to the format. The return value is a struct_time as returned by the gmtime() or localtime() method.

The format parameter uses the same directives as those used by the strftime() method. It defaults to "%a %b %d %H:%M:%S %Y" that matches the formatting returned by ctime().

If string cannot be parsed according to the format, or if it has excess data after parsing, ValueError is raised.

Syntax

The following below is the syntax for the Python Time strptime() method -

time.strptime(string[,  format] )

RECOMMENDED POST: Python Time altzone() Method with example

Parameter Details

  • string - This is the time in string format which would be parsed based on the given format.
  • format - This is the directive which would be used to parse the given string. The following are the directives which can be embedded on the format string -

Directive

  • %a - abbreviated weekday name
  • %A - full weekday name
  • %b - abbreviated month name
  • %B - full month name
  • %c - preferred date and time representation
  • %C - century Number (the year divided by 100, range 00 to 99)
  • %d - day of the month (01 to 31)
  • %D - same as %m%d%y
  • %e - day of the month (1 to 31)
  • %g - like %G, but without the century
  • %G - 4 digit year corresponding to the ISO week number (see %V)
  • %h - same as %b
  • %H - hour, using a 24-hour clock (00 to 23)
  • %I - hour, using a 12-hour clock (01 to 12)
  • %j - day of the year (001 to 366)
  • %m - month (01 to 12)
  • %M - minute
  • %n - newline character
  • %p - either am or pm based on given time value
  • %r - time in a.m. and p.m. notation
  • %R - time in 24 hour notation
  • %S - seconds
  • %t - tab character
  • %T - current time, equal to %H:%M:%S
  • %u - weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1.
  • %U - week number of the current year, starting with the first Sunday as the first day of the first week.
  • %V - the ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.
  • %W - week number of the current year, starting with the first Monday as the first day of the first week
  • %w - day of the week as a decimal, Sunday=0
  • %x - The preferred date representation without the time
  • %X - The preferred time representation without the date
  • %y - year without the century ( range 00 to 99)
  • %Y - year including the century
  • %Z or %z - time zone or name or abbreviation
  • %% - a literal % character

Return Value

It returns a struct_time as returned by gmtime() or localtime().

Example

The following below is a simple example -

#!/usr/bin/python
import time

struct_time
= time.strptime("30 Nov 00", "%d %b %y")
print "returned tuple: %s " % struct_time

Output
Below is the output of the above example -

returned tuple: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

RECOMMENDED POST: Python Time clock() Method with example

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Python Time time() method.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.


This post first appeared on Web Design Tutorialz, please read the originial post: here

Share the post

Python Time strptime() Method with example

×

Subscribe to Web Design Tutorialz

Get updates delivered right to your inbox!

Thank you for your subscription

×