一、说明
时间的获取及时间各格式间的转换是比较常用的操作,但一是多种语言经常容易弄混,二是同一种语言同一个功能可能有不同的实现函数,导致每次处理时间经常要百度所以来记录一下。
另外个人真不喜欢同样功能有多种写法的形式,从理想角度说多种实现方式让不同的人都能以其喜欢的方式进行编写;但实际上当你忘记的时候,你就总要怀疑是不是这么写、可不可以这么写、到底怎么写,然后到网上看又是五花八门的写法,这是个很耗费精力的事情。这也是我不喜欢Ruby的原因。
二、Python时间操作
2.1 获取时间对象
import time import datetime # 获取当前时间对象 # 返回形如datetime.datetime(2020, 2, 29, 10, 34, 36, 799216)对象 # 可通过对象的year、month、day、hour、minute、second、microsecond、tzinfo等属性获取各部分的信 datetime.datetime.now() datetime.datetime.today() # 获取昨天时间对象。当前时间减去一天的时间差即可 # 返回形如datetime.datetime(2020, 2, 28, 10, 37, 31, 470867) datetime.datetime.now() - datetime.timedelta(days=1) # 获取明天时间对象。当天的时间加上一天的时间差即可 # 返回形如datetime.datetime(2020, 3, 1, 10, 37, 31, 470867) datetime.datetime.now() - datetime.timedelta(days=1)
2.2 时间对象与给定格式转换
import time import datetime # 时间对象输出成指定格式 # 输出形如2020-02-29 11:03:29(-空格和:等符号可以换成其他任意自己想要的字符) obj = datetime.datetime.now() obj.strftime("%Y-%m-%d %H:%M:%S") obj.__format__("%Y-%m-%d %H:%M:%S") # 给定时间转成时间对象 datetime.datetime.strptime("2020-02-29 11:03:29", "%Y-%m-%d %H:%M:%S")
2.3 时间对象与时间戳转换
import time import datetime # 时间对象转成时间戳 # 结果形如1582943851.470867 obj = datetime.datetime.now() obj.timestamp() # 时间戳转成时间对象 datetime.datetime.fromtimestamp(time.time())
2.4 时间的比较
import time import datetime # 单纯地比较大小可以直接比较。 # 执行太快了两个对象是一样的,所以睡眠一下 obj_a = datetime.datetime.now() time.sleep(1) obj_b = datetime.datetime.now() if obj_b > obj_a: print("yes") # 要获取具体时间差,可将转成时间戳再相减 obj_a = datetime.datetime.now() time.sleep(1) obj_b = datetime.datetime.now() if obj_b > obj_a: late = obj_b.timestamp() - obj_a.timestamp() print(f"obj_b is late than b: {late}")
二、Shell时间操作
# 获取当前时间,并输出成指定格式 # 有引号是因为有空格,没有空格用不用引号都一样 date +"%Y-%m-%d %H:%M:%S" # 获取昨天时间,并输出成指定格式 date -d "yesterday" +"%Y-%m-%d %H:%M:%S" date -d "last-day" +"%Y-%m-%d %H:%M:%S" date -d "1 day ago" +"%Y-%m-%d %H:%M:%S" date -d "-1 days" +"%Y-%m-%d %H:%M:%S" # 获取明天日期,并输出成指定格式 date -d "tomorrow" +"%Y-%m-%d %H:%M:%S" date -d "next-day" +"%Y-%m-%d %H:%M:%S" date -d "+1 days" +"%Y-%m-%d %H:%M:%S"
三、MySQL时间操作
3.1 MySQL获取时间
# 获取当天日期 select curdate(); 获取昨天日期 select date_sub(curdate(),interval 1 day); 获取明天日期 select date_sub(curdate(),interval -1 day); # 获取当前时间 select now(); # 获取一个小前时间 select date_sub(now(),interval 1 hour); # 获取一个小时后时间 select date_sub(now(),interval -1 hour); # 获取昨天时间 select date_sub(now(),interval 1 day); # 获取明天时间 select date_sub(now(),interval -1 day);
3.2 时间输出成指定格式
# 当前时间输出成给定格式 select date_format(now(),"%Y%m%d %H:%i:%S"); # 给定时间输出成指定格式 select date_format(date_sub(curdate(),interval 1 day),"%Y%m%d"); select date_format(date_sub(now(),interval 1 hour),"%Y%m%d %H:%i:%S");
3.3 获取时间戳
select unix_timestamp(now());
四、附Python时间格式
Python、Shell、MySQL之间格式虽然大多是相同的,但小部分还是有区别,自己使用时要注意。比如分钟Python和Linux是"%H",但MySQL是"%i"。
Directive |
Meaning |
Example |
---|---|---|
%a
|
Weekday as locale’s abbreviated name. |
Sun, Mon, …, Sat (en_US); So, Mo, …, Sa (de_DE) |
%A
|
Weekday as locale’s full name. |
Sunday, Monday, …, Saturday (en_US); Sonntag, Montag, …, Samstag (de_DE) |
%w
|
Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. |
0, 1, …, 6 |
%d
|
Day of the month as a zero-padded decimal number. |
01, 02, …, 31 |
%b
|
Month as locale’s abbreviated name. |
Jan, Feb, …, Dec (en_US); Jan, Feb, …, Dez (de_DE) |
%B
|
Month as locale’s full name. |
January, February, …, December (en_US); Januar, Februar, …, Dezember (de_DE) |
%m
|
Month as a zero-padded decimal number. |
01, 02, …, 12 |
%y
|
Year without century as a zero-padded decimal number. |
00, 01, …, 99 |
%Y
|
Year with century as a decimal number. |
0001, 0002, …, 2013, 2014, …, 9998, 9999 |
%H
|
Hour (24-hour clock) as a zero-padded decimal number. |
00, 01, …, 23 |
%I
|
Hour (12-hour clock) as a zero-padded decimal number. |
01, 02, …, 12 |
%p
|
Locale’s equivalent of either AM or PM. |
AM, PM (en_US); am, pm (de_DE) |
%M
|
Minute as a zero-padded decimal number. |
00, 01, …, 59 |
%S
|
Second as a zero-padded decimal number. |
00, 01, …, 59 |
%f
|
Microsecond as a decimal number, zero-padded on the left. |
000000, 000001, …, 999999 |
%z
|
UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
|
(empty), +0000, -0400, +1030, +063415, -030712.345216 |
%Z
|
Time zone name (empty string if the object is naive). |
(empty), UTC, EST, CST |
%j
|
Day of the year as a zero-padded decimal number. |
001, 002, …, 366 |
%U
|
Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. |
00, 01, …, 53 |
%W
|
Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. |
00, 01, …, 53 |
%c
|
Locale’s appropriate date and time representation. |
Tue Aug 16 21:30:00 1988 (en_US); Di 16 Aug 21:30:00 1988 (de_DE) |
%x
|
Locale’s appropriate date representation. |
08/16/88 (None); 08/16/1988 (en_US); 16.08.1988 (de_DE) |
%X
|
Locale’s appropriate time representation. |
21:30:00 (en_US); 21:30:00 (de_DE) |
%%
|
A literal ‘%‘ character.
|
% |
参考:
https://docs.python.org/3/library/datetime.html#examples-of-usage-datetime
原文地址:https://www.cnblogs.com/lsdb/p/12382245.html