当前位置:网站首页>[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
边栏推荐
- 验证整数输入
- 成为比开发硬气的测试人,我都经历了什么?
- Programmer's debriefing report/summary
- Crawler text data cleaning
- Path and the largest
- 用户交互+格式化输出
- leetcode-128:最长连续序列
- Crypto Life, a day in the life of a Web3 project partner
- Chi-square distribution of digital image steganography
- What have I experienced when I won the offer of BAT and TMD technical experts?
猜你喜欢

GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13

tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现

Crypto Life, a day in the life of a Web3 project partner

Path and the largest

勾股数元组 od js

MySQL installation tutorial (detailed, package teaching package~)

一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例

力扣刷题之爬楼梯(7/30)

What does a software test report contain?

Basic introduction to ShardingJDBC
随机推荐
软件测试要达到一个什么水平才能找到一份9K的工作?
Nacos
程序员转正述职报告/总结
coldfusion8后台计划任务拿shell
MySQL的分页你还在使劲的limit?
Crypto Life, a day in the life of a Web3 project partner
[Map and Set] LeetCode & Niu Ke exercise
FPGA-based vending machine
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
Force buckled brush the stairs (7/30)
Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
uniapp uses 3rd party fonts
勾股数元组 od js
Is there a way to earn 300 yuan a day by doing a side business?
Google官方控件ShapeableImageView使用
Simple confession page
手把手教你配置Jenkins自动化邮件通知
MySQL的存储过程
16、注册中心-consul