如何确定python中的第一天的星期 [英] How to determine the first day of week in python

查看:428
本文介绍了如何确定python中的第一天的星期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据区域设置,我需要找到一周的第一天(星期日/星期一)
在JAVA我会做:

Based on the locale I need to find what is the first day of the week (Sunday/Monday) In JAVA I would do:

Calendar FR_cal = Calendar.getInstance(Locale.FRANCE);
Calendar CA_cal = Calendar.getInstance(Locale.CANADA);

DateFormatSymbols dfs = new DateFormatSymbols();
String weekdays[] = dfs.getWeekdays();

System.out.println(weekdays[JO_cal.getFirstDayOfWeek()]);
System.out.println(weekdays[FR_cal.getFirstDayOfWeek()]);

如何在python中执行?

How can I do it in python ?

推荐答案

我只能通过 Babel 了解如何执行此操作图书馆。可通过 easy_install 提供。

I could only figure out how to do this with the Babel library. It's available through easy_install.

>>> import babel
>>> locale = babel.Locale('en', 'US')
>>> locale.first_week_day
6
>>> locale.days['format']['wide'][locale.first_week_day]
u'Sunday'






证明以下无法工作,因为Nate足够指出。如果有人知道为什么,请张贴和回答显示如何做到正确。


turns out that the following doesn't work as Nate was kind enough to point out. If anybody knows why, please post and answer showing how to do it right. This should be doable with the standard library.

如果你只想要一天的日期,那么你可以使用日历。 LocalTextCalendar

If you just want the number of the day, then you can use calendar.LocalTextCalendar.

>>> import calendar
>>> c = calendar.LocaleTextCalendar(locale='de_DE') # locale=('en_US', 'UTF8') fails too.
>>> c.firstweekday
0

还有 iterweekdays 方法。

There is also the iterweekdays method.

>>> list(c.iterweekdays())
[0, 1, 2, 3, 4, 5, 6]

这篇关于如何确定python中的第一天的星期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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