当前位置:网站首页>[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
边栏推荐
- What is the ideal college life?
- What have I experienced when I won the offer of BAT and TMD technical experts?
- 软件测试报告有哪些内容?
- VSCode插件:嵌套注释
- Basic introduction to ShardingJDBC
- PDF split/merge
- 16、注册中心-consul
- JPEG Steganalysis of Digital Image Steganography
- What are the project management tools like MS Project
- rpm安装postgresql12
猜你喜欢

进程间通信学习笔记

pycharm cannot run after renaming (error: can't open file...No such file or directory)

Drools WorkBench的简介与使用

最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?

Can an inexperienced college graduate switch to software testing?my real case

软件测试工作3年了,谈谈我是如何从刚入门进阶到自动化测试的?

Force buckled brush the stairs (7/30)

1.非类型模板参数 2.模板的特化 3.继承讲解

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

Teach you how to configure Jenkins automated email notifications
随机推荐
Crypto Life, a day in the life of a Web3 project partner
Gateway routing configuration
如何在 go 程序中暴露 Prometheus 指标
[WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
kotlin中函数作为参数和函数作为返回值实例练习
Shell script to loop through values in log file to sum and calculate average, max and min
MySql installation and configuration super detailed tutorial and simple method of building database and table
Arbitrum Interview | L2 Summer, what does the standout Arbitrum bring to developers?
Nacos
两个有序数组间相加和的Topk问题
Between two orderly array of additive and Topk problem
Basic introduction to ShardingJDBC
软件测试基础接口测试-入门Jmeter,你要注意这些事
.NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
Chi-square distribution of digital image steganography
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
934. The Shortest Bridge
C语言小程序 -- 常见经典练习题