时间日期常用方法
python
from datetime import datetime, date, timedelta
def filter_time(time_format):
# '2022-06-27T17:30:31.96+08:00' -> '1656322231000'
if time_format:
return str(int(datetime.strptime(time_format, "%Y-%m-%dT%H:%M:%S.%f%z").timestamp()))+"000"
else:
return ""
def get_today_date():
return str(date.today())
def format_datetime_local(time_with_zone):
d = datetime.fromisoformat(time_with_zone)+timedelta(hours=8)
return d.isoformat(timespec='seconds', sep=" ").split('+')[0]
def getNowFormat_e8():
return (datetime.utcnow() + timedelta(hours=8)).isoformat(sep=' ',timespec='seconds')
def parse_datetime(time_format):
# Jun 1 05:46:49 2022 GMT
return datetime.strptime(time_format, '%b %d %H:%M:%S %Y GMT') + timedelta(hours=8)
def get_timeformat_with_zone():
t = (datetime.utcnow()+timedelta(hours=8)).isoformat(sep=" ",timespec="seconds")
return f"{t} +0800"
def get_date_format():
return date.today().isoformat()
def t_to_timeformat(t):
# str to time_format
t = int(t)/1000
return datetime.fromtimestamp(t).isoformat(sep=" ",timespec="microseconds")
def get_yesterday_date_format():
return (date.today()-timedelta(days=1)).strftime("%Y%m%d")
python
from datetime import datetime
def parse_timestamp(time_created):
# time_created = "\/Date(1680604245507)\/"
try:
timestamp = datetime.fromtimestamp(int(re.search('\d+', time_created).group()) / 1000.0)\
.isoformat(sep=" ", timespec='seconds')
return timestamp
except:
return ''
python
import re
from dateutil import parser
from datetime import timezone, timedelta
def parse_time_string_1(time_string):
# time_string = '2023-05-19T10:31:41.587456400Z'
try:
parsed_dt = parser.isoparse(time_string)
tz = timezone(timedelta(hours=8)) # tz = datetime.now().astimezone().tzinfo
local_dt = parsed_dt.astimezone(tz).strftime("%Y-%m-%d %H:%M:%S")
return local_dt
except ValueError:
return ''
def get_parse_time_string(time_string):
# time_string = 'The operating system is shutting down at system time 2023-05-19T10:31:41.587456400Z.'
match = re.search(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z', time_string)
if match:
time_string = match.group()
local_dt = parse_time_string_1(time_string)
return local_dt
return ''
result = get_parse_time_string('The operating system is shutting down at system time 2023-05-19T10:31:41.587456400Z.')
print(result)
python
import re
from datetime import datetime, timezone, timedelta
def parse_time_string_0(time_string):
# time_string = '2023-05-19T10:31:41.587456'
utc_dt = datetime.strptime(time_string, '%Y-%m-%dT%H:%M:%S.%f').replace(tzinfo=timezone.utc)
local_dt = utc_dt.astimezone(timezone(timedelta(hours=8))).strftime("%Y-%m-%d %H:%M:%S")
return local_dt
def get_parse_time_string(time_string):
# time_string = 'The operating system is shutting down at system time 2023-05-19T10:31:41.587456400Z.'
match = re.search(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z', time_string)
if match:
time_string = match.group()[:-4]
local_dt = parse_time_string_0(time_string)
return local_dt
return ''
result = get_parse_time_string('The operating system is shutting down at system time 2023-05-19T10:31:41.587456400Z.')
print(result)