>> All right. Let’s jump into the code
好了 来看代码
and see how the dates look when we use it inside our code.
看看在代码中怎样表示日期
So if we take a look at the code I have here,
先看一下这段代码
this is the code that allows me
我用这段代码
to retrieve the current date and time.
来获取当前的日期和时间
So you’ll see here there’s a line of code:
这里有一行代码:
from datetime import datetime.
from datetime import datetime
I said we’re going to talk about libraries a little bit later,
我们待会儿再讨论函数库
don’t stress out too much about that line of code.
这里不必深入讨论那行代码
It’s basically a way of saying, “Hey,
先混个脸熟
there’s a datetime function out there,
有这么个datetime函数
that’s pretty cool that someone else wrote for me.
太好了 有人帮我写好了
I want to use that, it’s in a library.”
我就用库中现成的那个
So this line of code allows me
这行代码方便了我
to use that other person’s code in my code.
可以将别人的代码 为己所用
And then, I have a line here, datetime.now.
再看这行代码datetime.now()
So what I’m doing is I’m calling the now function of datetime,
我正在调用datetime.now()
and that will return the current date.
它会反馈当前日期
and I’m storing that inside of variable.
我把变量放在里面
So now I have this variable current_date,
现在我们得到了这个变量current_date
which contains the current date and time,
它包含了当前的日期和时间
and that I’m doing a print statement.
我再写一个print语句
Let’s actually do this initially.
我们先写一个吧
We’re just saying print the current_date,
我们只用写输出current_date
just so you can see how that works,
这样你就知道它是怎么回事
and go down here and we’ll run that code,
我们到下面来运行一下这个代码
get_current_date.py, and, boom, code blows up.
get_current_date.py 然后 嘭 代码炸了
Remember we were talking about data types and type conversions?
还记得我们讲过的数据类型和类型转换吗?
Same things will come up when you’re working with dates.
处理日期时也要用到
So what happens is,
这里正好是
we see an error that says, I can only concatenate strings,
看这里的报错 只能拼接字符串
I don’t know how to concatenate datetimes.
无法拼接datetime
Yeah, I actually don’t know what, you know,
说真的 我不知道
5th of June plus Susan equals,
“6月5号”+“苏珊”等于什么
and neither does Python.
Python也不知道
So what we have to do is,
所以我们必须要
just like when we worked with numbers,
像处理数字一样
we have to convert the date into a string
把日期转化为字符串
because Python does know
因为Python知道
how to concatenate a string and a string together.
如何将字符串与字符串拼在一起
So by converting that to a string,
所以 将日期转成字符串后
now I successfully get a message: Today is…
现在我终于得到了: Today is…
and you can now have the secret,
你现在知道这个秘密了
you now know of Susan is recording this on the 10th of June,
无论你什么时候看到这些
no matter when you’re watching this.
你都知道这是苏珊6月10号写的
So another thing we can do
我们还可以做另一件事
once we have a variable stored in datetime object is
当我们将变量放在datetime对象中
that these functions that we can call that will act on the dates,
我们就可以调用这些能作用于日期的函数
and the functions are really magical.
这些函数真的很神奇
So what I’ve done now is I’ve added a second function
现在我引用第二个函数
that I want to call at the top and below in my code.
我想在代码的开始和最后调用它
So I’m still importing datetime,
我仍然引用datetime
but I’m calling another one called timedelta.
但是我调用了另一个叫timedelta的函数
That’s a neat little function
这是个很简洁的函数
that allows me to like, specify a period of time
我用来指定一段时间
like, x days, or five days, three days, one week, six weeks, and so on.
如 x天 或者5天 3天 1周 6周 等等
So I start off the same,
代码开头还是一样
I ask for the current date and time,
我要知道当前的日期和时间
and I’m putting that into a variable called today,
就把它放入一个名为today的变量中
and I’m displaying on the screen.
然后输出这个变量
So this is more or less the same code we just run,
这和我们刚才运行的代码差不多
but here’s the new code.
还有一种写法
I say, let’s use timedelta,
我们用timedelta函数来写
and say let’s add one day.
我们让它加一天
So I say, “Take timedelta, I’d like you to add a period of one day.”
来 用timedelta增加一天
So this is a parameter you pass in,
所以你该传入这个参数
I know that from looking up the documentation at the timedelta function,
我是通过查看timedelta函数的文档知道的
that’s how you learn how to change the values
以此来知道应如何改变值
whether you can pass in weeks, months,
无论是周数 还是月数
you would have to look at the timedelta function documentation.
都需要你查阅timedelta函数的文档
But what’s neat is now I can actually say,
但现在可以说 直接点儿
yesterday equals today minus one day.
昨天等于今天减去一天
So now I can actually do math on a date value
所以现在我们可以用日期进行计算
and then print that out on the screen,
然后输出在屏幕上
or I could even say down here lower,
或者甚至可以像下面这样写
let’s move comment this out for now just to keep it simple.
先把这些列为注释 方便看
So now, if I run this code,
现在 如果我运行这组代码
python date_functions.py,
python date_functions.py
you can see it says today is, it said nine.
你可以看到 它显示今天是9号
Doing this demo on the 10th of June,
做这个演示是在6月10号
it says yesterday was the ninth of June.
它显示昨天是6月9号
That capability of being able to subtract and add days is really, really powerful.
这个函数能加减天数 真的很强很好用
I once worked on some cool code,
我曾写过一些很炫酷的代码
it was a module about 1,500 lines long,
那是一个大约1500行的程序模块
that basically had to convert Julian dates to Gregorian dates
主要是要把儒略历转化为公历
to figure out if it was a leap year and things like that
以确定它是否是闰年
to basically do this.
来实现这一个函数的功能
So the fact that there’s a function we can call that does it for us,
所以实际上有这样一个函数能帮我们实现这个功能
it’s huge, it’s incredibly useful
这个函数功能强大 非常有用
if you’re doing anything with dates.
能解决任何时间上的问题
Let’s just go back to the code I want to show you,
让我们回到这组代码看看
it’s not limited to days.
它不限制天数
Here, I passed many days equals one,
这里我传入“days=1”
but I could have just as easily, we comment that out.
我们应该简单地 把注释断开
I have a line here and I say,
这里有一行代码
what if we subtract a week?
要是减一周呢?
So when I call the timedelta function,
所以当我调用timedelta函数时
I ask it to do a Delta of one week,
我让它生成1周的变量
and then I subtract one week from today.
然后我从今天减去1周
Well, now if I go and run that code,
好了 现在来运行这个代码
we can see it comes back and goes yep one week ago,
我们可以看到时间回到一周前了
if today is the 10th, one week ago was the third of June.
如果今天是10号 一周前就是6月3号
So and it takes care of all that stuff like was
它考虑到了所有的因素 比如
this month 30 days or 31 days,
这个月有30天或31天
and is it a leap year,
是否闰年
it’s all taken care of for me.
它都考虑到了
So timedelta is an example of one of the neat functions you can play with, with dates,
处理日期的实用函数很多 timedelta是其中一个
there’s a lot out there.
还有很多其他的
Suffice it to say,
可以这么说
if you want to do something with date, do a little searching,
在处理时间时 你先搜一下
you may find a function that does it for you
在你自己写之前
before you start coding it yourself.
就可能找到一个合适的函数
Now, when you displaying a date to somebody,
现在 当你向别人输出时间时
you probably need to format it.
可能要调整下格式
You may not always want the full date,
你不会总想输出完整的时间
hours, minutes, seconds, and milliseconds.
时 分 秒和毫秒
So if we take a look at this code here,
所以 看下这组代码
what I’ve done is,
我做的是
I’m actually extracting just the part of the date that I’m interested in.
只摘取日期中我想要的一部分
I start off again by saying I want to use the datetime function,
再说一次 我想用datetime函数
in the datetime library, so it’s such a great feature.
库中有现成的 这是python的一大特色
I ask for the current date and time by asking for datetime.now,
我导入datetime.now来获取当前的日期和时间
storing that in a variable called today.
并将它存在today变量中
Then what I’m doing is,
然后我要
if I asked today.day,
如果我写today.day
that will return just the day portion.
那就只会返回今天的日子
Today.month, the month portion,
输入today.month就是今天的月份
year of the year, hour of the hour, and so on.
年份 小时 等等同理
So then, I can take those values
然后 得到这些值
and display them all in the screen.
显示在屏幕上
So I can access whatever part of the date that I want,
因此 我可以得到想要的日期的任何部分
you’re not forced to display the full date every single time.
不必每次都显示完整的日期
So that way, you can extract the components you need
这样 你就可以摘出你需要的
and just use the parts you’re interested in.
和你想要的部分
Now, sometimes when you’re working with dates,
有时 当你处理日期时
you run into some interesting complications.
你会遇到一些有趣的难题
Let’s say you want to read a value that’s coming out of a file,
假设你想读取文件中的一个数值
or a database, or a user has given you a date.
或者数据库中的 用户给的日期中的
Unfortunately, most of the time
不巧的是 很多时候
when you read a date from a file, a database, or from a person,
从文件 数据库或个人信息中读取数据时
it will be treated as a string when it’s read by Python.
Python会把它当做一个字符串
But if I want to use these cool date functions,
可如果我想使用这些很酷的日期函数
I need it to be treated as a datetime data type.
就要将这些数据处理成datetime的数据类型
So once again, I’m going to have to do data type conversions.
所以我必须再一次做数据类型转换
Now, I need to convert from string to date,
现在 我需要把字符串转化为日期
but how do I know
但是我怎么知道
whether you gave me the date as day-month-year or month-day-year,
需要处理的日期是”日月年” 还是”月日年”
and did you use slashes or dashes?
用的斜线分隔 还是横线?
All these are extra complications.
这些都是意外情况
So I have some code here
我这里有一些代码
that will actually take a string and convert it to a date.
它主要用来获取字符串 并转化为日期
What you’ll see is, I ask somebody to enter their birthday.
你会看到 我让某人输入生日
So I’m using an input statement
这里我用input语句
and I tell them, “Please give me the date in this format.”
并告诉他们“日期请写这种格式”
The user may not read the instructions,
用户可能不会看说明
but I at least want to remind them the format I’m expecting.
但至少我想提醒他们用我期望的格式输入
Then what I do is,
然后
I use this strptime function.
我要用strptime这个函数
So what this does is, it strips elements out of the string
它能把符号从字符串中剔除
and converts it into a date-time format.
再将其转化为日期-时间格式
So you say, here’s the string, birthday
所以你看 这个生日的字符串
that contains the strings someone typed in
它包含了某人输入的字符串
and then I have to tell it what format I think the date’s going to be in.
我必须辨别里面的日期格式是怎样的
Percent d, day first, then month, percent m, then percent Y,
从日到月再到年
which means four digit year.
年份是四位数
How did I know those values?
我怎么知道这些值的呢
I looked up the documentation on the strptime function,
我查看了strptime函数的文档
that’s how you learn the syntax for this.
这样你就学到了这个语法结构
Then I can print out a value on the screen
然后我就能在屏幕上输出日期了
and I can use that timedelta function to say
并且我可以用timedelta函数写出
what was it the string one day before my birthday and all those great things.
我生日的前一天是哪天 等复杂问题
So let’s just try this out and see if it works.
我们来试一下 看它行不行
I’m going to go ahead and run my code,
我来运行一下代码
input date,
input_date.py
and it says, when is my birthday? Day/month/year,
它问 我的生日是?日/月/年
my birthday is clearly on the sixth of June, 1999.
我的生日肯定是06/06/1999
So I run, it says “your birthday is the 10th of June”,
然后运行 它显示“你的生日是6月10号”
and the day before your birthday was the fifth of June.
你生日的前一天是6月5号
So you can see I know it successfully converted it to a date
可以看出来 我知道它能成功转化成日期
because I’m able to use those datetime functions, that timedelta.
因为我很了解那些datetime函数 timedelta函数的使用
But what if the person who is running your code
但如果运行这个代码的人
isn’t paying attention?
不走心呢?
They say, “When’s my birthday?”
他们填“我的生日”时
“It’s the 30th of February.”
写成“30/2”
or maybe they just enter a valid date,
或只是输入了一个有效日期
but they enter dashes instead of slashes.
但中间是横线 而非斜杠
So 10-1999.
比如“10-1999”
So they enter something that doesn’t match that format
那他们输入的日期格式
that I’d said the date would be received in.
就不符合我的要求
Well, boom, here goes the code blowing up all over the place.
然后 嘭 代码又炸了
So this is one of those situations
这只是其中的一种情况
where you know obviously what’s going to happen occasionally in your code.
你很清楚它偶尔会出现故障
So what you should do here is add some exception handling.
所以你要做的就是 加一些异常处理预案
So I think maybe we should look at that in the next module.
我们可能会在下个模块中学习它
