strftime

datetime模块练习

我的未来我决定 提交于 2019-12-06 16:31:47
#_author:来童星#date:2019/12/6#1.获取当前日期import datetimeprint(datetime.date.today())# 2019-12-06#2.使用today和now获取当前日期和时间,时间精确到毫秒级print(datetime.datetime.today())# 2019-12-06 11:23:11.102894print(datetime.datetime.now())#2019-12-06 11:23:11.102893#3.使用strftime()格式化时间为标准格式#strftime可以将日期输出为我们想要的格式(要特别注意参数区分大小写),如:只输出日期print(datetime.datetime.now().strftime('%Y-%m-%d'))# 2019-12-06#如果输出当前日期和时间,精确到秒,设置日期时间参数即可print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))# 2019-12-06#如果输出当前日期和时间,星期,%A是星期全写的参数,%a是星期简写的参数print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %A '))# 2019-12-06 11:33:11

(二十七) python 3之time与datetime

青春壹個敷衍的年華 提交于 2019-12-06 14:10:51
time模块 time模块中时间表现的格式主要有三种:   a、timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量   b、struct_time时间元组,共有九个元素组。   c、format time 格式化时间,已格式化的结构使时间更具可读性。包括自定义格式和固定格式。 # 时间戳 t = time.time() print() # 1575517294.0607083 # 结构化时间 t = time.localtime() # time.struct_time(tm_year=2019, tm_mon=12, tm_mday=5, tm_hour=11, tm_min=41, tm_sec=34, tm_wday=3, tm_yday=339, tm_isdst=0) print(t.tm_year) # 2019 # 格式化时间 t = time.strftime("%Y-%m-%d %X", time.localtime()) print(t) # 2019-12-05 11:41:34 1、时间格式转换图: # 生成结构化时间 # 时间戳==>结构化时间 东8区 t1 = time.localtime() t2 = time.localtime(time.time()) print("t1==>{}".format(t1

strftime function in SQLite does not work

元气小坏坏 提交于 2019-12-06 12:05:31
问题 Hi, I am using the following query but it does not work, SELECT strftime('%m',Since) AS Month FROM Dates Type of Since column is DATETIME , but the result is shown an empty Month column without any error. 回答1: strftime isn't broken, it just doesn't understand the format of the datestamps in your database. SQLite version 3.7.9 2011-11-01 00:52:41 sqlite> create table dates (id int, d datetime); sqlite> insert into dates (id,d) values (1, date('now')); sqlite> insert into dates (id,d) values (2

python-time模块

北战南征 提交于 2019-12-06 10:19:43
时间模块 简介 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(日

Converting date formats python - Unusual date formats - Extract %Y%M%D

不打扰是莪最后的温柔 提交于 2019-12-06 08:39:33
I have a large data set with a variety of Date information in the following formats: DAYS since Jan 1, 1900 - ex: 41213 - I believe these are from Excel http://www.kirix.com/stratablog/jd-edwards-date-conversions-cyyddd YYDayofyear - ex 2012265 I am familiar with python's time module, strptime() method, and strftime () method. However, I am not sure what these date formats above are called on if there is a python module I can use to convert these unusual date formats. Any idea how to get the %Y%M%D format from these unusual date formats without writing my own calculator? Thanks. You can try

Python 时间,时间戳转换

霸气de小男生 提交于 2019-12-06 08:18:40
Time 模块 1. time模块,主要讲解time模块的时间,时间戳转换 1 def get_time(): 2 # time 模块时间,时间戳转换, "%Y-%m-%d %H:%M:%S" "年-月-日 时-分-秒" 3 import time 4 # 获取当前时间戳 5 now = int(time.time()) 6 now_13 = int(time.time()*1000) 7 # 转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S" 8 time_array = time.localtime(now) 9 style_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array) 10 # 融合到一起 11 other_style_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) 12 13 # 字符串时间转为时间戳 14 str_date = '2019-12-02 10:52:16' 15 # 先转换为时间数组 16 array_date = time.strptime(str_date, "%Y-%m-%d %H:%M:%S") 17 # 时间数组可调用函数,获取年份等...... 18 tm_year = array

python time.strftime %z is always zero instead of timezone offset

ぐ巨炮叔叔 提交于 2019-12-06 05:39:38
>>> import time >>> t=1440935442 >>> time.strftime("%Y/%m/%d-%H:%M:%S %z",time.gmtime(t)) '2015/08/30-11:50:42 +0000' >>> time.strftime("%Y/%m/%d-%H:%M:%S %z",time.localtime(t)) '2015/08/30-13:50:42 +0000' The offset stays the same +0000, but I expect '2015/08/30-13:50:42 +0200' The timezone is correct, as the command is interpreting capital %Z as it should >>> time.strftime("%Y/%m/%d-%H:%M:%S %Z",time.localtime(t)) '2015/08/30-13:50:42 CEST' Unix date works like I want $ date -u --date @1440935442 +"%Y/%m/%d-%H:%M:%S %z" 2015/08/30-11:50:42 +0000 $ date --date @1440935442 +"%Y/%m/%d-%H:%M:%S

21.time和random

左心房为你撑大大i 提交于 2019-12-06 05:32:51
time模块 三种时间表示 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。 格式化的时间字符串 元组(struct_time) : struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时) import time # 1 time() :返回当前时间的时间戳 time.time() #1473525444.037215 #---------------------------------------------------------- # 2 localtime([secs]) # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。 time.localtime() #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=11, tm_hour=0, # tm_min=38, tm_sec=39, tm_wday=6, tm_yday=255, tm_isdst=0) time.localtime(1473525444.037215) #---------

Need small help in converting date format in ruby

喜欢而已 提交于 2019-12-06 00:53:08
Here are some outputs: Date.today => Mon, 25 Jun 2012 Date.today.to_formatted_s(:long_ordinal) => "June 25th, 2012" Date.today.strftime('%A %d, %B') => "Monday 25, June" Now I need output in the format ie: Monday 25th, June or Thrusday, 1st, October Problem is to_formatted_s and strftime apply only on date and both or them return string. How can I get the output in the way I need. You can use Date::DATE_FORMATS to add a new customized format, and Integer.ordinalize to get the day ordinal: Date::DATE_FORMATS[:month_ordinal] = lambda { |date| date.strftime("%A #{date.day.ordinalize}, %B") } >>

python的time模块和datetime模块

折月煮酒 提交于 2019-12-06 00:26:19
1. 将当前时间转成字符串 strftime 方法,并输出 import datetime # 获取当前时间 datetime.datetime.now() print(datetime.datetime.now()) # 输出时间格式数据:2019-11-28 20:39:25.485711 now_time=str(datetime.datetime.now().strftime('%Y%m%d%H%M%S')) #将时间转化成字符串 print(now_time) 2. 将字符串形式的时间转成时间格式 time.strptime 方法,并获取时间戳 time.mktime 方法 import time time_str = '2019-09-02 00:00:01.002' #时间字符串 time_struct = time.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f") #得到时间格式数据,%f表示的是微秒 print(time_struct) time_stamp = int(time.mktime(time_struct)) #将时间格式数据转化成时间戳 print(time_stamp) 3. 将时间格式的数据转成字符串 #转格式,time_struct来自上面 time_ = time.strftime("%a %b %d %H