Python中的日期计算(Date calculations in Python)
我对Python相对比较陌生,并且正在尝试编写以下日期计算函数
- 查找指定日期时间/星期一的日期
- 在指定的日期时间内找到该月的第一个非周末日
- 在指定的日期时间内查找一年中的第一个非周末日
- 在指定的日期时间查找一个月的第N [星期几]
这是我迄今为止的尝试 - 如果逻辑可以改进(或纠正)为更多'Pythonic',请让我知道
import datetime def find_month_first_monday(tstamp = datetime.today()): day_of_month = datetime.date.today().timetuple()[2] day_of_week = datetime.weekday(tstamp) # now I have the dow, and dom, I can index into a 2D array of # dates for the month - IF I knew how to get to that array ... def find_first_gbd_in_month(tstamp = datetime.today()): # naive way would be to find the month and year from the passed in arg, # calculate the first day for that month/year and iterate until a non-weekend day # is found. Not elegant, there must be a better way pass def find_first_gbd_in_year(tstamp = datetime.today()): # Ditto, as above. pass def find_ndow_in_month(tstamp = datetime.today()): # again, I can get the month and the year from the passed in argument # what I need is a 2D array of dates for the month/year, so I can get # the nth dow (similar to reading off a calendar) pass
I am relatively new to Python, and I am experimenting with writing the following date calc functions
- find the date that is/was Monday for a specified datetime
- find the first non-weekend day of the month in a specified datetime
- find the first non-weekend day of the year in a specified datetime
- find the Nth [day of week] for a month in a specified datetime
Here are my attempts so far - if the logic can be improved (or corrected) to be more 'Pythonic', please let me know
import datetime def find_month_first_monday(tstamp = datetime.today()): day_of_month = datetime.date.today().timetuple()[2] day_of_week = datetime.weekday(tstamp) # now I have the dow, and dom, I can index into a 2D array of # dates for the month - IF I knew how to get to that array ... def find_first_gbd_in_month(tstamp = datetime.today()): # naive way would be to find the month and year from the passed in arg, # calculate the first day for that month/year and iterate until a non-weekend day # is found. Not elegant, there must be a better way pass def find_first_gbd_in_year(tstamp = datetime.today()): # Ditto, as above. pass def find_ndow_in_month(tstamp = datetime.today()): # again, I can get the month and the year from the passed in argument # what I need is a 2D array of dates for the month/year, so I can get # the nth dow (similar to reading off a calendar) pass
原文:https://stackoverflow.com/questions/3194036
最满意答案
那么,@Guffa已经充分回答了。 如果你愿意,你可以接受他的答案。 :)
我只想添加另一件事,这是jQuery版本之间的冲突。 这是因为如果你使用的插件与你包含的jQuery版本不兼容(发生在我身上)。 您可以添加另一个与该插件兼容的jQuery版本,以及使用jQuery.noConflict()
Well, @Guffa already answered sufficiently. You can accept his answer if you want. :)
I just want to add another thing, which is the conflict between jQuery versions. It's because if you use plugins that are not compatible with the jQuery version you've included (happened to me some time). You can add another jQuery version that is compatible with that plugin, along with using jQuery.noConflict()
相关问答
更多-
那么,@Guffa已经充分回答了。 如果你愿意,你可以接受他的答案。 :) 我只想添加另一件事,这是jQuery版本之间的冲突。 这是因为如果你使用的插件与你包含的jQuery版本不兼容(发生在我身上)。 您可以添加另一个与该插件兼容的jQuery版本,以及使用jQuery.noConflict() Well, @Guffa already answered sufficiently. You can accept his answer if you want. :) I just want to add ...
-
jQuery和BlockUI对象不支持这个属性或方法(jQuery and BlockUI Object doesn't support this property or method)[2022-04-28]
你确定blockUI插件脚本实际上被加载吗? Are you sure that the blockUI plugin script is actually being loaded? -
jQuery对象不支持IE中的属性或方法trim()(jQuery Object doesn't support property or method trim() in IE)[2022-02-15]
IE没有string.trim()方法。 相反,您可以调用jQuery的$.trim(str) 。 IE doesn't have a string.trim() method. Instead, you can call jQuery's $.trim(str). -
你应该不是在调用gMap吗? Shouldn't you be calling gMap instead?
-
一个问题是.val()返回一个字符串,打破了jQuery链,这意味着你不能再应用任何jQuery方法( .trim() )。 另一个问题是trim是jQuery对象本身的一种方法,其中要修剪的值必须作为参数传递( $.trim(value) ),而不是链接到jQuery函数调用的方法( $(value).trim() )。 获取修剪长度值的正确代码如下: $.trim($(this).val()).length 编辑:这个答案假设ECMAscript 5的字符串String.prototype.trim( ...
-
“对象不支持属性或方法” - jQuery Nokia Lumia(“Object doesn't support property or method” - jQuery Nokia Lumia)[2023-06-22]
我最后一次去找出来了! 我&&'before' in $(m)添加到“if”语句中作为附加检查,以查看'before'函数/属性/方法是否也存在。 I had one last go and figured it out! I added &&'before' in $(m) to the "if" statement as an additional check to see if the 'before' function/property/method exists also. -
“对象不支持此属性或方法”错误 - JQuery(“Object doesn't support this property or method” error - JQuery)[2024-03-01]
您需要将IE8上的jQuery,错误:对象不支持此属性或方法(jQuery on IE8, error: Object doesn't support this property or method)[2022-06-18]
导致错误的jQuery(缩小版本)中的行是: 第140行: var C = Bb.exec(u) 解决方案是将此行更改为: var C = Bb.exec(u.toString()) @kirilloid是正确的,因为Regexp正在抛出错误,特别是当匹配的值是数字时。 例如,动画css属性,如不透明度,顶部,左侧等。将其转换为字符串可以解决问题。 现在IE没有再收到任何错误。 自从我编辑jQuery库文件以来,我不太喜欢这个修复程序,但我想我将不得不使用它。 此更改不必对正常功能产生任何不良副作用。 Th ...使用JQuery 1.7的IE8“对象不支持此属性或方法”(IE8 “Object doesn't support this property or method” with JQuery 1.7)[2023-01-07]
方法“indexOf”不是IE8的数组方法的一部分。 您应该能够使用jQuery.inArray快速解决这个问题: http ://api.jquery.com/jQuery.inArray/ 更改: states.indexOf($("[name='payToBankState']").val()) < 0 至: $.inArray($("[name='payToBankState']").val(), states) < 0 The method "indexOf" is not part of I ...对象不支持此属性或方法IE8 Jquery [关闭](Object doesn't support this property or method IE8 Jquery [closed])[2022-07-11]
.toogle可能是.toggle的拼写错误 .toogle is probably a misspelling of .toggle相关文章
更多- 探索 Python,第 1 部分: Python 的内置数值类型
- Python资源索引 【转载】
- python下载pps视频
- python字典操作
- python top project of 2013
- 【转帖】Python 资源索引
- Python 字符串操作
- (转)Python WEB应用框架纵览
- Python内建函数(B)
- 用Python编写Nagios Hadoop监控脚本
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)