python 时间戳转日期 不自动补零 without zero-padding
2024-10-21 10:06:29
1. 时间戳转日期
- 代码
import time
timestamp = 1568171336
time_format = "%Y-%m-%d %H:%M:%S"
time_local = time.localtime(timestamp)
new_date = time.strftime(time_format, time_local)
print(new_date)
- 结果
2019-09-11 11:08:56
2. 不自动补零
对于Linux 需要在字段类型前加上 -
对于win 需要再字段类型前加上 #
- 代码示例
import time
timestamp = 1568171336
time_format = "%Y-%#m-%#d %H:%M:%S"
time_local = time.localtime(timestamp)
new_date = time.strftime(time_format, time_local)
print(new_date)
- 结果
2019-9-11 11:08:56
来自 Python datetime formatting without zero-padding
Accepted answer not a proper solution (IMHO) The proper documented methods:
In Linux "#" is replaced by "-":
%-d, %-H, %-I, %-j, %-m, %-M, %-S, %-U, %-w, %-W, %-y, %-Y
In Windows "-" is replaced by "#":
%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y
- Linux
mydatetime.strftime('%-m/%d/%Y %-I:%M%p')
- Windows
mydatetime.strftime('%#m/%d/%Y %#I:%M%p')
最新文章
- Login控件尝试
- iOS 含有 中文的URL 转码问题
- Android端百度地图API使用详解
- selenium在chrome上运行报 Element is not clickable at point (1096, 26)
- 【转】MyBatis学习总结(七)——Mybatis缓存
- Java学习----集合函数
- Jenkins api java 调用
- [php基础]PHP.INI配置:文件上传功能配置教程
- Bug解决过程复盘
- 【Java】ArrayList 的 toArray() 方法抛出 ClassCastException 异常
- spring容器启动的加载过程(三)
- 团队项目汇总beta
- Python:list 和 array的对比以及转换时的注意事项
- angular4.0运行在微信端的坑坑洼洼
- 富文本框vue-quill-editor的使用
- iOS ReplayKit 录屏 框架的使用
- Windows 7 编译64位boost库
- RabbitMq中的消息应答与持久化
- 【PowerDesigner】【1】简单介绍
- [C/C++] 字符串错题集