1import datetime
2
3today = datetime.datetime.now()
4date_time = today.strftime("%m/%d/%Y, %H:%M:%S")
5print("date and time:",date_time)
1
2from datetime import datetime
3
4now = datetime.now() # current date and time
5
6year = now.strftime("%Y")
7print("year:", year)
8
9month = now.strftime("%m")
10print("month:", month)
11
12day = now.strftime("%d")
13print("day:", day)
14
15time = now.strftime("%H:%M:%S")
16print("time:", time)
17
18date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
19print("date and time:",date_time)
20
21
22-------------------------------------------------------------------------
23Directive Meaning Example
24%a Abbreviated weekday name. Sun, Mon, ...
25%A Full weekday name. Sunday, Monday, ...
26%w Weekday as a decimal number. 0, 1, ..., 6
27%d Day of the month as a zero-padded decimal. 01, 02, ..., 31
28%-d Day of the month as a decimal number. 1, 2, ..., 30
29%b Abbreviated month name. Jan, Feb, ..., Dec
30%B Full month name. January, February, ...
31%m Month as a zero-padded decimal number. 01, 02, ..., 12
32%-m Month as a decimal number. 1, 2, ..., 12
33%y Year without century as a zero-padded decimal number. 00, 01, ..., 99
34%-y Year without century as a decimal number. 0, 1, ..., 99
35%Y Year with century as a decimal number. 2013, 2019 etc.
36%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
37%-H Hour (24-hour clock) as a decimal number. 0, 1, ..., 23
38%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
39%-I Hour (12-hour clock) as a decimal number. 1, 2, ... 12
40%p Locale’s AM or PM. AM, PM
41%M Minute as a zero-padded decimal number. 00, 01, ..., 59
42%-M Minute as a decimal number. 0, 1, ..., 59
43%S Second as a zero-padded decimal number. 00, 01, ..., 59
44%-S Second as a decimal number. 0, 1, ..., 59
45%f Microsecond as a decimal number, zero-padded on the left. 000000 - 999999
46%z UTC offset in the form +HHMM or -HHMM.
47%Z Time zone name.
48%j Day of the year as a zero-padded decimal number. 001, 002, ..., 366
49%-j Day of the year as a decimal number. 1, 2, ..., 366
50%U Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01, ..., 53
51%W Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. 00, 01, ..., 53
52%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
53%x Locale’s appropriate date representation. 09/30/13
54%X Locale’s appropriate time representation. 07:06:05
55%% A literal '%' character. %
56-------------------------------------------------------------------------
57
1
2<?php
3$date = new DateTime('2000-01-01');
4echo $date->format('Y-m-d H:i:s');
5?>
6
7
1The program below converts a datetime object containing current date and time to different string formats.
2
3Code:
4
5from datetime import datetime
6
7now = datetime.now() # current date and time
8
9year = now.strftime("%Y")
10print("year:", year)
11
12month = now.strftime("%m")
13print("month:", month)
14
15day = now.strftime("%d")
16print("day:", day)
17
18time = now.strftime("%H:%M:%S")
19print("time:", time)
20
21date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
22print("date and time:",date_time)
23
24Output after run the code:
25year: 2020
26month: 03
27day: 31
28time: 04:59:31
29date and time: 03/31/2020, 04:59:31
30
31Here, year, day, time and date_time are strings, whereas now is a datetime object.
1| Directive | Meaning | Example |
2|-----------|------------------------------------------------------------------------------------------|
3|%a | Abbreviated weekday name. | Sun, Mon, .. |
4|%A | Full weekday name. | Sunday, Monday, ... |
5|%w | Weekday as a decimal number. | 0, 1, ..., 6 |
6|%d | Day of the month as a zero-padded decimal. | 01, 02, ..., 31 |
7|%-d | Day of the month as a decimal number. | 1, 2, ..., 30 |
8|%b | Abbreviated month name. | Jan, Feb, ..., Dec |
9|%B | Full month name. | January, February, ... |
10|%m | Month as a zero-padded decimal number. | 01, 02, ..., 12 |
11|%-m | Month as a decimal number. | 1, 2, ..., 12 |
12|%y | Year without century as a zero-padded decimal number. | 00, 01, ..., 99 |
13|%-y | Year without century as a decimal number. | 0, 1, ..., 99 |
14|%Y | Year with century as a decimal number. | 2013, 2019 etc. |
15|%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, ..., 23 |
16|%-H | Hour (24-hour clock) as a decimal number. | 0, 1, ..., 23 |
17|%I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, ..., 12 |
18|%-I | Hour (12-hour clock) as a decimal number. | 1, 2, ... 12 |
19|%p | Locale’s AM or PM. | AM, PM |
20|%M | Minute as a zero-padded decimal number. | 00, 01, ..., 59 |
21|%-M | Minute as a decimal number. | 0, 1, ..., 59 |
22|%S | Second as a zero-padded decimal number. | 00, 01, ..., 59 |
23|%-S | Second as a decimal number. | 0, 1, ..., 59 |
24|%f | Microsecond as a decimal number, zero-padded on the left. | 000000 - 999999 |
25|%z | UTC offset in the form +HHMM or -HHMM. | |
26|%Z | Time zone name. | |
27|%j | Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 |
28|%-j | Day of the year as a decimal number. 1, 2, ..., 366 | |
29|%U | Week number of the year (Sunday as the first day of the week). | 00, 01, ..., 53 |
30|%W | Week number of the year (Monday as the first day of the week). | 00, 01, ..., 53 |
31|%c | Locale’s appropriate date and time representation. | Mon Sep 30 07:06:05 2013|
32|%x | Locale’s appropriate date representation. | 09/30/13 |
33|%X | Locale’s appropriate time representation. | 07:06:05 |
34|%% | A literal '%' character. | % |
35
1from datetime import datetime
2
3datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')