strftime

Django model数据 时间格式

自作多情 提交于 2019-11-30 15:03:00
from datetime import datetime dt = datetime.now() print '时间:(%Y-%m-%d %H:%M:%S %f): ' , dt.strftime( '%Y-%m-%d %H:%M:%S %f' ) print '时间:(%Y-%m-%d %H:%M:%S %p): ' , dt.strftime( '%y-%m-%d %I:%M:%S %p' ) print '星期缩写%%a: %s ' % dt.strftime( '%a' ) print '星期全拼%%A: %s ' % dt.strftime( '%A' ) print '月份缩写%%b: %s ' % dt.strftime( '%b' ) print '月份全批%%B: %s ' % dt.strftime( '%B' ) print '日期时间%%c: %s ' % dt.strftime( '%c' ) print '今天是这周的第%s天 ' % dt.strftime( '%w' ) print '今天是今年的第%s天 ' % dt.strftime( '%j' ) print '今周是今年的第%s周 ' % dt.strftime( '%U' ) print '今天是当月的第%s天 ' % dt.strftime( '%d' ) ''' 输出如下: -----

python 中的strptime()和strftime()

纵然是瞬间 提交于 2019-11-29 21:34:03
python 中的strptime()和strftime() 转自:https://blog.csdn.net/qq_39348113/article/details/82528851 strptime(): 功能:按照特定时间格式将字符串转换(解析)为时间类型。 示例如下: def date_try(date): date = datetime.datetime.strptime(date,'%Y-%m-%d') return date def main(): a = date_try('2016-02-29') print(a) main() 1 2 3 4 5 6 7 8 9 输出如下: 2016-02-29 00:00:00 Process finished with exit code 0 1 2 3 可见,在这段代码中,strptime()函数将字符串‘2016-02-29’解析为时间。 如果我将这段字符串改为‘2016-02-30’后,超出了2月份的天数极限,运行就会报错 def date_try(date): date = datetime.datetime.strptime(date,'%Y-%m-%d') return date def main(): a = date_try('2016-02-30') print(a) main() 1 2 3 4 5 6

Rails: How to make Date strftime aware of the default locale?

久未见 提交于 2019-11-29 21:13:24
I have my default locale set in the environment.rb as de (German). I also see all the error messages in German, so the locale is picked up by the server. But when I try to print date with strftime like following: some_date.strftime('%B, %y') It prints in English ( January, 11 ), and not the expected German ( Januar, 11 ). How can I print the date according to the default locale? Milan Novota Use the l (alias for localize ) method instead of raw strftime, like this: l(date, format: '%B %d, in the year %Y') See here for more information, hope that helps. You can also define 'named' formats, a

python 时间模块小结(time and datetime)

ⅰ亾dé卋堺 提交于 2019-11-29 18:23:05
一:经常使用的时间方法 1.得到当前时间 使用time模块,首先得到当前的时间戳 In [42]: time.time() Out[42]: 1408066927.208922 将时间戳转换为时间元组 struct_time In [43]: time.localtime(time.time()) Out[43]: time.struct_time(tm_year=2014, tm_mon=8, tm_mday=15, tm_hour=9, tm_min=42, tm_sec=20, tm_wday=4, tm_yday=227, tm_isdst=0) 格式化输出想要的时间 In [44]: time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) Out[44]: '2014-08-15 09:43:04' 接上文,不加参数时,默认就是输出当前的时间 In [48]: time.strftime('%Y-%m-%d %H:%M:%S') Out[48]: '2014-08-15 09:46:53’ 当然也可以透过datetime模块来实现,如下: In [68]: t = time.time() In [69]: datetime.datetime.fromtimestamp(t).strftime('%Y-

python--Time(时间)模块

跟風遠走 提交于 2019-11-29 18:21:33
要使用一个模块,首先要把模块导入进来 import time 我们先把这一篇文章需要用的模块导入进来 首先说一下time模块,time模块中的函数 --sleep:休眠指定的秒数(可以是小数) import time print("开始") time.sleep(5) print("结束")#如果在pycharm中输入这段代码,从开始到结束,中间会停留5秒,sleep后面的括号里面填的数字就是秒数 --time:获取时间戳 import time t = time.time() print(t) #这段代码是获取时间戳(从1970-01-01 00:00:00到你输入代码运行时的秒数) --localtime:将时间戳转换为对象 import timelocal_time = time.localtime() print(local_time) #会打印出此时此刻的年月日时分秒 print(local_time.tm_year) #指定输出对应的年份 print(local_time[0]) #也可以指定下标输出年份 mktime:根据年月日等信息转换为时间戳 import timenew_time = time.mktime((2099, 9, 9, 10, 10, 10, 1, 212, 0)) #上面括号里面是填写自定义的时间,要写够9个数字 print(new_time)

python time模块

自闭症网瘾萝莉.ら 提交于 2019-11-29 13:57:15
时间模块 简介 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。 时间间隔是以秒为单位的浮点小数。 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。 #常用方法 1.time.sleep(sesc) #(线程)推迟指定的时间运行,单位为秒 2.time.time() # 获取当前时间戳(开始为1970年1月1日伦敦时间 结束为现在) time模块 介绍 : time模块是最原始的时间模块,可以在获取数据的基础上进行各种操作,还有一个datetime模块,是在time模块的基础上有了更好的功能 表示时间的三种方式 时间戳(timestamp): 通常来说,时间戳表示的是伦敦时间自1970年1月1日00:00:00开始计算的时间偏移量,运行 type(time.time) 返回的一个float类型 结构化时间(struct_time): struct_time在python中是一个元组,元组中共有9个元素(分别是,年,月,日,时,分,秒,一年中第几周,一年中第几天,已经是否夏令时) 索引(Index ) 属性(Attribute) 值(Values) 0 tm_year(年) 比如2011 1 tm_mon(月) 1 - 12 2 tm_mday(日

How to parse a string to a ctime struct?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 12:47:49
Is there an established way to parse a string to a Time structure? What I would ideally like to do is the reverse of strftime(...) , where instead of producing a string from a time struct and format string, I get a time struct from a string parsed according to the provided format string. I would prefer not to add additional overhead by including a DateTime class such as that found in Boost or .NET The corresponding method for parsing strings to struct tm * is strptime . Unfortunately this isn't part of the standard C runtime library and doesn't exist on e.g. windows, but you can reuse a BSD

python中datetime模块中strftime/strptime函数

时光总嘲笑我的痴心妄想 提交于 2019-11-29 10:20:40
f== format     p==parse 1、获取当前时间(日期格式) from datetime import datetime datetime.now()#输出  datetime.datetime(2019, 9, 12, 20, 17, 15, 426867) 2、由日期格式转化为字符串格式的函数为: datetime.datetime.strftime() datetime.now().strftime('%b-%m-%y %H:%M:%S')#输出'Sep-09-19 20:12:56' 3、由字符串格式转化为日期格式的函数为: datetime.datetime.strptime() datetime.strptime('Sep-09-19 20:12:56','%b-%d-%y %H:%M:%S') #输出为: datetime.datetime(2019, 9, 9, 20, 12, 56) 4、两个函数都涉及日期时间的格式化字符串,列举如下: %a 星期几的简写;如 星期三为Web %A 星期几的全称;如 星期三为Wednesday %b 月份的简写; 如4月份为Apr %B 月份的全称; 如4月份为April %c 标准的日期的时间串;(如: 04/07/10 10:43:39) %C 年份的后两位数字 %d 十进制表示的每月的第几天 %D 月/天/年

python对日期的操作

允我心安 提交于 2019-11-29 08:26:05
import datetime print ((datetime.datetime.now() - datetime.timedelta(days = 1 )).strftime( "%Y-%m-%d %H:%M" )) print ((datetime.datetime.now() - datetime.timedelta(minutes = 1 )).strftime( "%Y-%m-%d %H:%M" )) print ((datetime.datetime.now() - datetime.timedelta(seconds = 1 )).strftime( "%Y-%m-%d %H:%M" ))   输出结果 以下是随机获取15天前的日期和时间实例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import datetime import random d = random.randint( 0 , 15 ) date = ((datetime.datetime.now() - datetime.timedelta(days = d)).strftime( "%Y-%m-%d %H:%M:%S" )) # print date day = date[ 0 : 11 ] #

Making Django forms.DateField show use local date format

大城市里の小女人 提交于 2019-11-29 03:38:37
问题 I'm trying to find an easy way to build forms which show dates in the Australian format (dd/mm/yyyy). This was the only way I could find to do it. It seems like there should be a better solution. Things to note: Created a new widget which renders date value in dd/mm/yyyy format Created new date field to prepend the locate date format string to the list of values it tries to use I imagine the ideal solution would be a datefield which automatically localised but that didn't work for me