Datetime in Python

Datetime in Python

Datetime in Python, in this article i will explain how to use date and time in Python .

Date and time 

  1. Before using the  date and time you will need to import the datetime library.
  2. In the sample program below “from datetime import datetime ”  shows  the command import the  date and time Library.
  3. “myBirthDay =datetime.strptime(“1982:07:24″,”%Y:%m:%d”)”this command shows formatting of the date and time .you can always change the style format according to your needs.

Sample Code

 


from datetime import datetime

delta = datetime.now() -datetime(1990,6,25)


print(delta)

myBirthDay =datetime.strptime("1982:07:24","%Y:%m:%d")

print(myBirthDay)


 

Formatting Table

No Code Meaning Example
1 %a Weekday abbreviated name. Tues
2 %A Weekday full name. Tuesday
3 %w Weekday decimal number, Sundayis 0 and 6 is Saturday. 2.
4 %d Day of the month in decimal number. 30
5 %-d Day of the month in decimal number. (Platform specific) 30
6 %b Month as locale’s abbreviated name. sep
7 %B Month as locale’s full name. September
8 %m Month in decimal number. 09
9 %-m Month in decimal number. (Platform specific) 9
10 %y Year without century 18
11 %Y Year with century 2018
12 %H Hour (24-hour clock) 08
13 %-H Hour (24-hour clock) (Platform specific) 8
14 %I Hour (12-hour clock) 08
15 %-I Hour (12-hour clock)(Platform specific) 8
16 %p AM 0r PM PM
17 %M Minute as a zero-padded decimal number. 08
18 %-M Minute (Platform specific) 8
19 %S second in decimal number. 08
20 %-S second in decimal number.(Platform specific) 8
21 %f Microsecond as a decimal number 000000
22 %j Day of the year as a zero-padded decimal number. 300
23 %-j Day of the year as a zero-padded decimal number(Platform specific). 300
24 %U Week number of the year 30
25 %W Week number of the year 30
26 %x appropriate date representation. 09/31/18
27 %X appropriate time representation. 07:06:05

 

Check out a Simple Python Program here

Leave a Reply

Your email address will not be published. Required fields are marked *

14 − eight =