当前位置:网站首页>[1154] How to convert string to datetime
[1154] How to convert string to datetime
2022-07-31 01:50:00 【Zhou Xiaodong】
#!/usr/bin/env python# -*- coding:utf-8 -*-import datetimeimport time# datetime stringst = "2017-11-23 16:10:10"# current date and timedt = datetime.datetime.now()# current timestampsp = time.time()# 1. Convert datetime to stringdef datetime_toString(dt):print("1. Convert datetime to string: ", dt.strftime("%Y-%m-%d %H:%M:%S"))# 2. Convert the string to datetimedef string_toDatetime(st):print("2. Convert the string to datetime: ", datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S"))# 3. Convert the string to timestamp formdef string_toTimestamp(st):print("3. Convert the string to timestamp form:", time.mktime(time.strptime(st, "%Y-%m-%d %H:%M:%S")))# 4. Convert timestamp to string formdef timestamp_toString(sp):print("4. Convert timestamp to string form: ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sp)))# 5. Convert the datetime type to the timestamp formdef datetime_toTimestamp(dt):print("5. Convert datetime type to timestamp form:", time.mktime(dt.timetuple()))# 1. Convert datetime to stringdatetime_toString(dt)# 2. Convert the string to datetimestring_toDatetime(st)# 3. Convert the string to timestamp formstring_toTimestamp(st)# 4. Convert timestamp to string formtimestamp_toString(sp)# 5. Convert the datetime type to the timestamp formdatetime_toTimestamp(dt)
Experiment results:
1. Convert datetime to string: 2017-11-23 17:05:18
2. Convert string to datetime: 2017-11-23 16:10:10
3. ConvertConvert string to timestamp format: 1511424610.0
4. Convert timestamp to string format: 2017-11-23 17:05:18
5. Convert datetime type to timestamp format: 1511427918.0
Reference: https://www.php.cn/python-tutorials-420250.html
边栏推荐
猜你喜欢
ROS Action通信
最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
After reading "MySQL Database Advanced Practice" (SQL Xiao Xuzhu)
Basic introduction to ShardingJDBC
coldfusion文件读取漏洞(CVE-2010-2861)
VSCode Plugin: Nested Comments
What have I experienced when I won the offer of BAT and TMD technical experts?
mysql 视图
Are you still working hard on the limit of MySQL paging?
leetcode-399: division evaluation
随机推荐
验证 XML 文档
软件测试工作3年了,谈谈我是如何从刚入门进阶到自动化测试的?
934. The Shortest Bridge
Basic introduction to ShardingJDBC
leetcode-1161: Maximum in-layer element sum
leetcode-128:最长连续序列
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
MySQL installation tutorial (detailed, package teaching package~)
android的webview缓存相关知识收集
PDF split/merge
简易表白小页面
leetcode-128: longest continuous sequence
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
pc端判断当前使用浏览器类型
Overview of prometheus monitoring
MySQL的存储过程
"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
kotlin中函数作为参数和函数作为返回值实例练习
Charging effect simulation
What have I experienced when I won the offer of BAT and TMD technical experts?