需要帮助比较日期 [英] Need Help comparing dates

查看:42
本文介绍了需要帮助比较日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,正在开发我的第一个程序。我想把我在网站上找到的日期与今天的日期进行比较。我有问题

是网站只显示3个字母的月份名称和日期。

示例:6月15日


如何我会把它与另一个日期进行比较吗?

我的程序的目的是加载一个网页,看看前面的内容是否是新的或陈旧的,而不是几天。

正确方向的任何帮助都会很棒!

解决方案

co *********** @ gmail.com 写道:

我是Python的新手,正在开发我的第一个程序。我正在尝试将我在网站上找到的日期与今天的日期进行比较。我有的问题是网站只显示3个字母的月份名称和日期。
示例:6月15日




''datetime' '标准库中的模块将完成

的工作,创建可以比较的日期对象。


< URL:http://docs.python .org / lib / module-datetime>


使用datetime.date()从任意值构造日期,使用datetime.date获取

当前日期。今天()。这些

函数返回的对象可以直接比较。


至于如何从字符串表示到日期对象,

你现在正在谈论解析字符串以提取日期/时间

信息。这在标准库中没有提供,主要是因为没有一种明显的方法,因此没有b $ b。现已提出的增加此类功能的PEP

已被撤销:


< URL:http://www.python.org/dev/peps/ pep-0321 />


从字符串解析日期时间值是模糊的,容易出现大量的b
假设和错误,因此程序员必须选择自己的集合

假设。 egenix''mxDateTime'模块的一个特定的一组特定的b
假设的一个流行实现。


< URL:http://www.egenix .com / files / python / mxDateTime.html>


该模块早于(并且很大程度上是灵感)标准

库''datetime''模块;但是,我不知道egenix模块是否使用可以与标准库中的对象一起使用的对象。


-

\没有理由让任何人想要一台电脑在他们的| b / b $ \\'家中。 - Ken Olson,总裁,董事长兼创始人|

_o__)数字设备公司,1977年|

Ben Finney


>我是Python新手,正在开发我的第一个程序。我正在尝试

将我在网站上找到的日期与今天的日期进行比较。我有的问题是网站只显示3个月的姓名和日期。
示例:6月15日


没有年份,对吗?您是否假设今年是当年的


我如何将其与另一个日期进行比较?




一旦你把它们作为日期,

from datetime import date


您可以像对待任何其他同类商品一样比较它们。


如果您需要将月份字符串映射回实际日期,你

可以使用这本词典:

month_numbers = dict([(date(2006,m,1).strftime("%b"),m)
范围内的m(1,13)])


它恰好是特定于语言环境的,所以你可能需要修改一下

位''映射与网站

使用的不同。我也假设案例是相同的(相比

比试图标准化为大写/小写)


然后,你可以使用

webpageDateString =" 3月21日>
webMonth,webDay = webpageDateString.split()
月= m [webMonth]
day = int(webDay)
网页日期= date(date.today()。year,month,day)
compareDate = date.today()
compareDate< webpageDate
False compareDate> webpageDate
True


你可以将负载包装在一个函数中,比如

def isNewer(dateString,year = date.today()。年):
.... monthString,dayString = dateString.split()

.... month = month_numbers [monthString]

.... day = int(dayString)

.... return date.today()<日期(年,月,日)


允许你这样做

isNewer(" 7月1日)
True isNewer(" ; 4月1日)



False


等。


日期时间模块中有很多好东西。


-tkc


Ben Finney< bi **************** @ benfinney.id.au>写道:

co ********** *@gmail.com 写道:

我是Python的新手,正在开发我的第一个程序。我正在尝试将我在网站上找到的日期与今天的日期进行比较。我有的问题是网站只显示3个字母的月份名称和日期。
示例:6月15日



日期时间模块在标准库将完成创建可比较日期对象的工作。

< URL:http://docs.python.org/lib/module-datetime>

使用datetime.date()从任意值构造日期,使用datetime.date.today()获取
当前日期。这些
函数返回的对象可以直接比较。

至于如何从字符串表示到日期对象,
你现在谈论解析字符串提取日期/时间信息。这在标准库中没有提供




我一发送这个,就记得标准库*确实*

提供日期时间解析:


< URL:http://docs.python.org/lib/module-time#l2h-1956>


所以你的任务现在包括:


- 使用datetime.date.today获取今天日期的日期对象()

- define解析字符串日期的格式

- 从time.strptime()获取一个struct_time对象,为它提供

字符串和格式

- 对日期缺失部分做出任何假设(例如

年)

- 通过将值提供给datetime.date获得日期对象()

- 比较两个约会对象


-

\嘿荷马!你迟到了英语! " PFF!英语,谁需要|

` \那?我永远不会去英格兰! - 巴尼&荷马,_The |

_o__)辛普森一家_ |

Ben Finney


I am new to Python and am working on my first program. I am trying to
compare a date I found on a website to todays date. The problem I have
is the website only shows 3 letter month name and the date.
Example: Jun 15

How would I go about comparing that to a different date? The purpose of
my program is to load a webpage and see if the content on the front
page is fresh or stale as in older than a few days. Any help in the
right direction would be great!

解决方案

co***********@gmail.com writes:

I am new to Python and am working on my first program. I am trying
to compare a date I found on a website to todays date. The problem I
have is the website only shows 3 letter month name and the date.
Example: Jun 15



The ''datetime'' module in the standard library will do the job of
creating date objects that can be compared.

<URL:http://docs.python.org/lib/module-datetime>

Construct a date from arbitrary values with datetime.date(), get the
current date with datetime.date.today(). The objects returned by those
functions can be compared directly.

As for how to get from a string representation to a date object,
you''re now talking about parsing strings to extract date/time
information. This isn''t provided in the standard library, largely
because there''s no "one obvious way to do it". An existing PEP
proposing adding such functionality has since been withdrawn:

<URL:http://www.python.org/dev/peps/pep-0321/>

Parsing datetime values from strings is fuzzy and prone to lots of
assumptions and errors, so the programmer must choose their own set of
assumptions. One popular implementation of a specific set of
assumptions is the egenix ''mxDateTime'' module.

<URL:http://www.egenix.com/files/python/mxDateTime.html>

That module predates (and was largely an inspiration for) the standard
library ''datetime'' module; however, I don''t know if the egenix module
uses objects that can be used with those from the standard library.

--
\ "There is no reason anyone would want a computer in their |
`\ home." -- Ken Olson, president, chairman and founder of |
_o__) Digital Equipment Corp., 1977 |
Ben Finney


> I am new to Python and am working on my first program. I am trying to

compare a date I found on a website to todays date. The problem I have
is the website only shows 3 letter month name and the date.
Example: Jun 15
No year, right? Are you making the assumption that the year is
the current year?
How would I go about comparing that to a different date?



Once you''ve got them as dates,

from datetime import date
you can just compare them as you would any other comparable items.

If you need to map the month-strings back into actual dates, you
can use this dictionary:
month_numbers = dict([(date(2006, m, 1).strftime("%b"), m) for m in range(1,13)])

It happens to be locale specific, so you might have to tinker a
bit if you''re mapping comes out differently from what the website
uses. I also made the assumption the case was the same (rather
than trying to normalize to upper/lower case)

Then, you can use
webpageDateString = "Mar 21"
webMonth, webDay = webpageDateString.split()
month = m[webMonth]
day = int(webDay)
webpageDate = date(date.today().year, month, day)
compareDate = date.today()
compareDate < webpageDate False compareDate > webpageDate True

You can wrap the load in a function, something like
def isNewer(dateString, year = date.today().year): .... monthString, dayString = dateString.split()
.... month = month_numbers[monthString]
.... day = int(dayString)
.... return date.today() < date(year, month, day)

which will allow you to do
isNewer("Jul 1") True isNewer("Apr 1")


False

and the like.

There''s plenty of good stuff in the datetime module.

-tkc



Ben Finney <bi****************@benfinney.id.au> writes:

co***********@gmail.com writes:

I am new to Python and am working on my first program. I am trying
to compare a date I found on a website to todays date. The problem I
have is the website only shows 3 letter month name and the date.
Example: Jun 15



The ''datetime'' module in the standard library will do the job of
creating date objects that can be compared.

<URL:http://docs.python.org/lib/module-datetime>

Construct a date from arbitrary values with datetime.date(), get the
current date with datetime.date.today(). The objects returned by those
functions can be compared directly.

As for how to get from a string representation to a date object,
you''re now talking about parsing strings to extract date/time
information. This isn''t provided in the standard library



As soon as I sent this, I remembered that the standard library *does*
provide datetime parsing:

<URL:http://docs.python.org/lib/module-time#l2h-1956>

So your task now consists of:

- get a date object of today''s date using datetime.date.today()
- define a format for parsing a string date
- get a struct_time object from time.strptime() feeding it the
string and the format
- make any assumptions about missing pieces of the date (e.g. the
year)
- get a date object by feeding values to datetime.date()
- compare the two date objects

--
\ "Hey Homer! You''re late for English!" "Pff! English, who needs |
`\ that? I''m never going to England!" -- Barney & Homer, _The |
_o__) Simpsons_ |
Ben Finney


这篇关于需要帮助比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆