python 时间戳转日期 不自动补零 without zero-padding
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')
python 时间戳转日期 不自动补零 without zero-padding的更多相关文章
- Python时间戳和日期的相互转换
Python时间戳和日期的相互转换 (2014-03-17 11:24:35) 转载▼ 分类: Python 当前时间戳:time.time() 当前日期:time.ctime() 1.Pytho ...
- 【c++基础】int转string自动补零
前言 使用to_string函数可以将不同类型的数据转换为string类,请参考here和here.如果string的位数固定,如何进行自动补零呢?请看本文实例! 代码 确定位数,to_string ...
- Java中在数字前自动补零方法
/** * 数字前面自动补零 * @param number 数字 * @return */ public static String geFourNumber(int number){ Number ...
- js正则格式化日期时间自动补0
原文 js正则格式化日期时间自动补0 背景 时间日期格式化的需求很常见,也有很多工具类转换方法,比如需要将2022-3-4这种日期格式转化为2022-03-04,也就是实现个位数月份或天数日期自动前置 ...
- Python 在 Terminal 中的自动补全
为了在 Terminal 中使用 Python 更加方便,在 home 目录下添加脚本 .pythonstartup,内容如下, 然后在 .bashrc 中添加 export PYTHONSTARTU ...
- Python交互模式下代码自动补全
这个功能是以lib的形式提供的,配置写到home下的.pythonrc文件中, 并设置好环境变量让python启动时执行初始化: # ~/.pythonrc # enable syntax compl ...
- 在Python命令行和VIM中自动补全
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1. VIM下的配置: wget https://github.com/rkulla/pydiction/arc ...
- java 数字前自动补零实现
/** * 里数字转字符串前面自动补0的实现. * */ public class TestStringFormat { public static void main(String[] args) ...
- python机器学习实现人脸图片自动补全
人脸自动补全 关注公众号"轻松学编程"了解更多. 1.导包 import matplotlib.pyplot as plt import numpy as np import pa ...
- python数字前自动补零
>>> '%d' % 23 #输出23 ' >>> '%5d' % 23 #输出的数字前有3个空位,共占5个字符 ' >>> '%05d' % 2 ...
随机推荐
- 直播平台搭建源码,canvas 画一条波浪线 进度条
直播平台搭建源码,canvas 画一条波浪线 进度条 <template> <view> <canvas :style="{'width': width ...
- :)模型保存为单一个pb文件
模型保存为单一个pb文件 背景 参考连接: https://www.yuque.com/g/jesse-ztr2k/nkke46/ss4rlv/collaborator/join?token=XUVZ ...
- macos下编译glfw
环境准备 # macos 环境配置,需要预装xcode和cmake brew install cmake # 下载源码 git clone https://github.com/glfw/glfw.g ...
- Redis容器的二种常用启动方式
#==========redis:latest镜像的容器启动命令============# docker run -d --name redis01 -p 6379 --restart unless- ...
- python多线程的问题
参考:https://stackoverflow.com/questions/20939299/does-python-support-multithreading-can-it-speed-up-e ...
- Vulnhub 靶场 LOOZ: 1
Vulnhub 靶场 LOOZ: 1 前期准备: 靶机地址:https://www.vulnhub.com/entry/looz-1,732/ kali攻击机ip:192.168.147.190 靶机 ...
- base64 转图片
data:image+jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg% ...
- 全国分乡镇第五次人口普查数据shp数据库省市区县街道
==名称:全国第五次人口普查任意十个乡镇的乡镇界地图==数据说明:精确到乡镇级别.==数据:面数据,属性列表(乡镇名.乡镇代码,第六次人口普查分乡镇属性数据)==时间:截止2000年12月31日==格 ...
- python pip 下载出问题解决方法(超时出错)
问题如下, 百度了一下找到了解决方法 这样的原因是连接不到国外的安装源,在下载的代码行中手动输入国内安装源 例如: pip install itchat -i https://pypi.tuna.ts ...
- docker方式安装awvs和nessus渗透工具
docker-compose.yaml文件 version: '2' services: awvsnessus: image: leishianquan/awvs-nessus:v4 environm ...