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 ...
随机推荐
- ubuntu22.04安装 kubernetes(docker)
初始化检查 操作系统:ubuntu22.04 LTS docker:20.10.18 kubelet: v1.23.6 kubeadm:v1.23.6 kubectl: v1.23.6 1.校准时间: ...
- JS 代码片段 / 预编译/预解析 /执行上下文/ECG/EC/ECS/GO/VO/AO
代码段概念 一个 script 就是一个代码段 在一个页面中可以有多个代码段 每一个代码段, 彼此独立的, 如果上面的代码段报错了, 不会影响下一个代码段 referenceError 引用错误 下面 ...
- Win10家庭版找不到组策略gpedit.msc怎么办
Win10家庭版找不到组策略gpedit.msc怎么办 @echo off pushd "%~dp0" dir /b %systemroot%\Windows\servicing\ ...
- RabbitMQ 修改密码
C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.14\sbin> rabbitmqctl.bat change_password adm ...
- Nlog连接密码隐藏
- BUUCTF-[极客大挑战 2019]Http
一道考察http请求头X-Forwarded-For字段和Referer字段User-Agent字段的题目 一.基础知识 X-Forwarded-For(XFF)又名XFF头 1)概述:X-Forwa ...
- Centos安装后出现please make your choice from '1' to eter the license information spoke | 'q' to quit |'c' to continue |'r' to refresh
这是要求用户阅读或者接收协议: 解决方法:输入"1",按Enter键 阅读许可协议,输入"2",按Enter键 接受许可协议,输入"q" ...
- DELL品牌电脑开机显示supportASsiSt丨pre-Boot SyStem Proforman?
这个问题,我百度了N个网站,得到的结果都是针对老版本BIOS的 https://wen.baidu.com/question/1647310335599401700.html https://zhua ...
- CF513F2 题解
题意 传送门 有 \(a+b+1\) 个会动的棋子,在一个大小为 \(n\times m\) 的棋盘上,棋盘上有一些点有障碍.棋子中,有 \(a\) 个红色棋子,\(b\) 个蓝色棋子,和 \(1\) ...
- 【剑指Offer】【树】二叉树的镜像
题目:操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 1 ...