Python模块,它可以转换大量的方法来用字符串表示时间来对对象进行初始化。
text2datetime的Python项目详细描述
python模块,它可以将大量用字符串表示时间的方法转换为datetime对象
一些示例包括:
“明早5:00”-明早5:00
“+3年+2天”-3年零2天
“-5mo 16:00”-适用于5个月前的16:00(下午4点)
“1/8/2015”-根据月前的国旗,可能是2015年1月8日或8月1日。
支持转换所有这些文本的主要函数是text2datetime.text2datetime
text2datetime方法
def text2datetime(timeStr, now=None, monthBeforeDay=True):
timestr-要转换为datetime.datetime对象的文本
now-默认为now,但如果在此处提供datetime.datetime,则可以计算相对于其他日期的值
MonthBeforeDay-对于相关格式,true将具有预期的格式为月前一天(美国格式),false将具有月前一天(欧洲格式)。
其他方法
虽然text2datetime支持解释所有已知形式的日期和时间字符串,但您也可以为每个表单使用单独的公共方法。
所有方法的pydoc见http://htmlpreview.github.io/?https://github.com/kata198/text2datetime/blob/master/doc/text2datetime.html。
支持的格式
日期应采用以下形式之一:
Relative Modifiers
Relative modifiers represent a delta from current time, current date.
Each modifier starts with a direction (+ or -), then a number, then a unit.
Example: +3d means ‘3 days from this very second’
Available modifiers:
y = years
yr = years
mo = months
d = days
h = hours
m = minutes
s = seconds
您可以使用相对修饰符,并将最终条目设置为固定时间,
例如“+3d 12:00:00”将是三天后的中午。
固定字符串
One of the following fixed strings:
“now” - Right now (to the second)
“today” - Beginning of today (00:00:00)
“tomorrow” - Beginning of tomorrow
“yesterday” - Beginning of yesterday
可以选择后跟时间,例如小时:分钟或小时:分钟:秒,否则使用午夜(00:00:00)。时钟是24小时制,00=午夜,除非“PM”或“AM”在末尾。
示例:“明天下午5:00”
ctime格式
Ctime format with optional day of week
(3-letter-day) [3-letter-month] [2-digit date] [2-hour]:[2-minute]:[2-second] [4-digit Year]
示例:2015年1月28日星期三12:28:13
美国日期格式
numeric Month/Day/Year with optional time as hour:minute or hour:minute:second. Clock is a 24-hour clock, 00=midnight, unless “PM” or “AM” is at the end.
e.x.: 1/28/2015 or 1/28/2015 12:28:13
This is used when monthBeforeDay=True (default) in text2datetime, getDatetimeFromDateStr when monthBeforeDay=True, and getDatetimeFromAmericanTime
欧洲日期格式
numeric Day/Month/Year with optional time as hour:minute or hour:minute:second. Clock is a 24-hour clock, 00=midnight, unless “PM” or “AM” is at the end.
e.x.: 28/1/2015 or 28/1/2015 12:28:13
This is used when monthBeforeDay=False in text2datetime, getDatetimeFromDateStr when monthBeforeDay=False, and getDatetimeFromEuropeanTime
仅时间
time as hour:minute or hour:minute:second will use current date. Clock is a 24 hour clock, 00=midnight, unless “PM” or “AM” is at the end.
注释:
Unless AM/PM is specified in formats that support it, hours are in 24-hour clock format, starting with 00=midnight, 23 = 11PM