当前位置:网站首页>[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
边栏推荐
- 验证整数输入
- How to expose Prometheus metrics in go programs
- VSCode插件:嵌套注释
- Kyushu cloud as cloud computing standardization excellent member unit
- Is there a way to earn 300 yuan a day by doing a side business?
- MySQL (6)
- leetcode-128: longest continuous sequence
- What have I experienced when I won the offer of BAT and TMD technical experts?
- Centos 7.9 install PostgreSQL14.4 steps
- MySQL的安装教程(嗷嗷详细,包教包会~)
猜你喜欢
Kyushu cloud as cloud computing standardization excellent member unit
VSCode Plugin: Nested Comments
Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
Can an inexperienced college graduate switch to software testing?my real case
软件测试工作3年了,谈谈我是如何从刚入门进阶到自动化测试的?
16、注册中心-consul
12张图带你彻底搞懂服务限流、熔断、降级、雪崩
最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
成为比开发硬气的测试人,我都经历了什么?
随机推荐
【AcWing 62nd Weekly Game】
JPEG Steganalysis of Digital Image Steganography
CV-Model【3】:MobileNet v2
Arbitrum Interview | L2 Summer, what does the standout Arbitrum bring to developers?
PDF 拆分/合并
【AcWing 第62场周赛】
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
Path and the largest
16、注册中心-consul
Charging effect simulation
想要写出好的测试用例,先要学会测试设计
MySql installation and configuration super detailed tutorial and simple method of building database and table
VSCode插件:嵌套注释
观察者(observer)模式(一)
最大路径和
软件测试工作3年了,谈谈我是如何从刚入门进阶到自动化测试的?
Distributed. Idempotency
Nacos
Interprocess communication study notes
leetcode-1161:最大层内元素和