1 00:00:00,000 --> 00:00:06,576 So we've got our time, date or datetime objects, what now? 2 00:00:06,576 --> 00:00:09,725 When we print out, let's say, 3 00:00:09,725 --> 00:00:15,218 a datetime we get a string in the default Python format. 4 00:00:15,218 --> 00:00:19,503 But what if we want to format this date a certain way? 5 00:00:19,503 --> 00:00:26,683 Perhaps the American month-day-year or the Australian day-month-year. 6 00:00:26,683 --> 00:00:32,907 We might want our date as a number and the month as a word like August 8. 7 00:00:32,907 --> 00:00:37,336 Or maybe we want all numbers to be zero-padded and 8 00:00:37,336 --> 00:00:41,035 separated with a slash like 01/02. 9 00:00:41,035 --> 00:00:45,913 Well, datetime comes with methods to help us with formatting and 10 00:00:45,913 --> 00:00:49,125 parsing our datetime objects. 11 00:00:49,125 --> 00:00:56,670 First, we have stringftime() or String Format Time. 12 00:00:56,670 --> 00:01:02,773 This is a method that is available to time, date and datetime objects. 13 00:01:02,773 --> 00:01:06,290 This method converts an instance of one of 14 00:01:06,290 --> 00:01:10,920 these objects to a string according to a given format. 15 00:01:10,920 --> 00:01:17,540 This means you'll need to call it on an object that we have already created. 16 00:01:17,540 --> 00:01:24,502 The syntax looks like this object.strftime(format) 17 00:01:24,502 --> 00:01:29,287 where format tells Python how we want to 18 00:01:29,287 --> 00:01:33,790 format our time, date or datetime. 19 00:01:33,790 --> 00:01:40,691 In the teacher's notes, you'll find a link to Python's documentation on format codes. 20 00:01:40,691 --> 00:01:45,653 Each of these directives, a percentage sign plus a letter, 21 00:01:45,653 --> 00:01:50,530 represents a certain element in a date, time or datetime. 22 00:01:50,530 --> 00:01:55,788 For example, it says here that %H is a zero-padded 23 00:01:55,788 --> 00:02:02,610 number from 0 to 23 that represents the hour on a 24 hour clock. 24 00:02:02,610 --> 00:02:05,480 Let's see these in action. 25 00:02:05,480 --> 00:02:07,755 First, let's create some objects. 26 00:02:07,755 --> 00:02:13,738 t = datetime.time(hour = 11, 27 00:02:13,738 --> 00:02:16,440 minute = 3), 28 00:02:16,440 --> 00:02:22,809 d = datetime.date(2023, 8, 29 00:02:22,809 --> 00:02:32,460 16) and dt = datetime.datetime.combine (d, t). 30 00:02:32,460 --> 00:02:36,011 Now, let's see strftime in action. 31 00:02:36,011 --> 00:02:42,696 t.strftime("%H:%M") will 32 00:02:42,696 --> 00:02:46,579 give us 11:03. 33 00:02:46,579 --> 00:02:53,929 d.strftime("%a %d/%m/%Y") 34 00:02:53,929 --> 00:03:02,543 will give us Wednesday 16/08/2023. 35 00:03:02,543 --> 00:03:07,424 And dt.strftime("%M 36 00:03:07,424 --> 00:03:12,302 minutes past %I %p on %B 37 00:03:12,302 --> 00:03:18,512 %d") will give us 03 minutes 38 00:03:18,512 --> 00:03:23,849 past 11 AM on August 16. 39 00:03:23,849 --> 00:03:28,737 Using this reference, you can piece together all sorts 40 00:03:28,737 --> 00:03:33,529 of formats for your time, date and datetime objects. 41 00:03:33,529 --> 00:03:38,405 You may also come across other format code lists in your 42 00:03:38,405 --> 00:03:42,017 Python journey such as strftime.org. 43 00:03:42,017 --> 00:03:49,710 My only warning is this, some lists offer format codes that are platform specific. 44 00:03:49,710 --> 00:03:54,281 This means that this format code might only work for 45 00:03:54,281 --> 00:03:57,478 MacOS or Windows and not the other. 46 00:03:57,478 --> 00:04:02,463 Remember, when we're creating apps for clients or users one day, 47 00:04:02,463 --> 00:04:07,370 we must be as neutral as possible in terms of the operating system. 48 00:04:07,370 --> 00:04:11,695 My recommendation is to avoid using these platform 49 00:04:11,695 --> 00:04:15,119 specific format codes where possible. 50 00:04:15,119 --> 00:04:22,910 Next, we have datetime.strptime() or string parse time. 51 00:04:22,910 --> 00:04:27,288 This method does the opposite of strftime(). 52 00:04:27,288 --> 00:04:33,970 It parses a string into a datetime object according to a given format. 53 00:04:33,970 --> 00:04:39,329 Now this is only available in the datetime class. 54 00:04:39,329 --> 00:04:48,493 The syntax looks like this datetime.strptime(datestr, format). 55 00:04:48,493 --> 00:04:53,439 datestr is simply a string with our datetime information that 56 00:04:53,439 --> 00:04:56,680 we want to parse into a datetime object. 57 00:04:56,680 --> 00:05:02,350 For example, 27 September 2022 or 58 00:05:02,350 --> 00:05:07,859 16:00 16/08/2023. 59 00:05:07,859 --> 00:05:12,759 Then, we're already familiar with format. This section uses 60 00:05:12,759 --> 00:05:14,920 a string of format codes 61 00:05:14,920 --> 00:05:20,920 so Python can match up our date string to what each section means. 62 00:05:20,920 --> 00:05:23,219 Let's try it out. 63 00:05:23,219 --> 00:05:27,923 Make sure we've imported datetime, 64 00:05:27,923 --> 00:05:36,866 datetime.datetime.strptime("27 September 2022", 65 00:05:36,866 --> 00:05:43,467 %d ,%B ,%Y") and that gives us a datetime. 66 00:05:43,467 --> 00:05:46,199 Let's try it again 67 00:05:46,199 --> 00:05:54,401 with datetime.datetime.strptime(''16:00 68 00:05:54,401 --> 00:05:59,012 16/08/2023", And 69 00:05:59,012 --> 00:06:05,171 the format is %H:%M %d/%m/%Y") 70 00:06:06,651 --> 00:06:11,241 And there we have it, another datetime with all of the correct 71 00:06:11,241 --> 00:06:14,519 information that we had in our date string. 72 00:06:14,519 --> 00:06:20,400 String parse time is usually used when we have to parse user input. 73 00:06:20,400 --> 00:06:24,527 For example, when an app asks user to let's say, 74 00:06:24,527 --> 00:06:28,373 enter their birthday, or an events start time, 75 00:06:28,373 --> 00:06:33,280 we then convert this into an object that we can use, a date time.