gpt4 book ai didi

python - 在 Windows 中获取用户日期格式

转载 作者:行者123 更新时间:2023-12-01 07:18:50 25 4
gpt4 key购买 nike

我的问题类似于this one ,但是,我不想获取locale的日期格式,而是获取用户设置的日期格式。原因是我正在使用 pywin32 api 与 Outlook 通信,特别是按日期过滤邮件。我必须将日期传递给应用程序的格式是用户设置的格式,否则它将无法工作。

所以代码如下所示:

inbox = outlook.Session.GetSharedDefaultFolder(user, 6)

mails = inbox.Items.Restrict(
"[ReceivedTime] > '{}'".format(retrieve_from.strftime(local_date_fmt))
)

如果有人知道如何在不以用户定义的格式传递日期的情况下执行此操作,那也可以解决我的问题。

编辑:

我指的是在此窗口的区域设置中设置的短日期格式:

Region Settings window

因此在这种情况下,local_date_fmt 将设置为 %Y-%m-%d

最佳答案

您可以使用[MS.Learn]: GetDateFormatW function (datetimeapi.h) ,由 [ME.TimGolden]: win32api.GetDateFormat 包裹.

示例:

>>> import time
>>> import pywintypes as pwts
>>> import win32api as wapi
>>>
>>> DATE_SHORTDATE = 0x00000001
>>> DATE_LONGDATE = 0x00000002
>>>
>>> cur_time = time.time()
>>> cur_time
1567692860.7315922
>>> time.ctime(cur_time)
'Thu Sep 5 17:14:20 2019'
>>>
>>> wapi.GetDateFormat(0, DATE_SHORTDATE, pwts.Time(cur_time))
'2019-09-05'
>>>
>>> wapi.GetDateFormat(0, DATE_LONGDATE, pwts.Time(cur_time)) # Bonus
'Thursday, 5 September, 2019'

关于python - 在 Windows 中获取用户日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57805392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com