当前位置:网站首页>[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
边栏推荐
- The PC side determines the type of browser currently in use
- ShardingJDBC基本介绍
- GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
- 曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
- Crawler text data cleaning
- MySQL installation tutorial (detailed, package teaching package~)
- 计算S=a+aa+…+aa…a
- Shell script to loop through values in log file to sum and calculate average, max and min
- Crypto Life, a day in the life of a Web3 project partner
- rpm install postgresql12
猜你喜欢

华为od 转骰子 js

Centos 7.9 install PostgreSQL14.4 steps

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

MySql installation and configuration super detailed tutorial and simple method of building database and table

关于Redis相关内容的基础学习

Are you still working hard on the limit of MySQL paging?

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

leetcode-952:按公因数计算最大组件大小

Google官方控件ShapeableImageView使用

Shell变量与赋值、变量运算、特殊变量
随机推荐
Crawler text data cleaning
两个有序数组间相加和的Topk问题
PDF split/merge
934. The Shortest Bridge
软件测试基础接口测试-入门Jmeter,你要注意这些事
leetcode-952:按公因数计算最大组件大小
keep-alive cache component
力扣刷题之有效的正方形(每日一题7/29)
MySQL (6)
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
coldfusion8后台计划任务拿shell
1.非类型模板参数 2.模板的特化 3.继承讲解
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
基于FPGA的图像实时采集
Nacos
keep-alive缓存组件
C语言小程序 -- 常见经典练习题
力扣每日一题-第46天-704. 二分查找
软件测试报告有哪些内容?