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的更多相关文章

  1. Python时间戳和日期的相互转换

    Python时间戳和日期的相互转换 (2014-03-17 11:24:35) 转载▼   分类: Python 当前时间戳:time.time() 当前日期:time.ctime() 1.Pytho ...

  2. 【c++基础】int转string自动补零

    前言 使用to_string函数可以将不同类型的数据转换为string类,请参考here和here.如果string的位数固定,如何进行自动补零呢?请看本文实例! 代码 确定位数,to_string ...

  3. Java中在数字前自动补零方法

    /** * 数字前面自动补零 * @param number 数字 * @return */ public static String geFourNumber(int number){ Number ...

  4. js正则格式化日期时间自动补0

    原文 js正则格式化日期时间自动补0 背景 时间日期格式化的需求很常见,也有很多工具类转换方法,比如需要将2022-3-4这种日期格式转化为2022-03-04,也就是实现个位数月份或天数日期自动前置 ...

  5. Python 在 Terminal 中的自动补全

    为了在 Terminal 中使用 Python 更加方便,在 home 目录下添加脚本 .pythonstartup,内容如下, 然后在 .bashrc 中添加 export PYTHONSTARTU ...

  6. Python交互模式下代码自动补全

    这个功能是以lib的形式提供的,配置写到home下的.pythonrc文件中, 并设置好环境变量让python启动时执行初始化: # ~/.pythonrc # enable syntax compl ...

  7. 在Python命令行和VIM中自动补全

    作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1. VIM下的配置: wget https://github.com/rkulla/pydiction/arc ...

  8. java 数字前自动补零实现

    /** * 里数字转字符串前面自动补0的实现. * */ public class TestStringFormat { public static void main(String[] args) ...

  9. python机器学习实现人脸图片自动补全

    人脸自动补全 关注公众号"轻松学编程"了解更多. 1.导包 import matplotlib.pyplot as plt import numpy as np import pa ...

  10. python数字前自动补零

    >>> '%d' % 23 #输出23 ' >>> '%5d' % 23 #输出的数字前有3个空位,共占5个字符 ' >>> '%05d' % 2 ...

随机推荐

  1. fastjson场景

    json转java对象 // 将Json字符串通过fastjson转为JSONObject对象 JSONObject jsonObject = JSONObject.parseObject(userJ ...

  2. Panel容器中显示多个窗体并通过按钮实现窗体切换

    Panel容器中显示多个窗体并通过按钮实现窗体切换 在项目开发中经常会有如下需求: 主窗体formMain中有个一Panle: 在Panel内显示多个窗体,如form1,form2--,分别通过不同按 ...

  3. clickHouse安装踩坑

    1.查看版本  clickhouse-server --version 2.下载 linux机器上 wget下载,有两个安装包体积较大 wget https://packages.clickhouse ...

  4. Android蓝牙固件升级 DFU-OTA 固件升级

    1.添加 依赖包: implementation 'no.nordicsemi.android:dfu:1.11.0' 2.DfuService类继承  DfuBaseService package ...

  5. Doris使用记录

    创建分区表: CREATE TABLE `test_partitioned` ( `gmt_create` datetime NULL COMMENT "创建时间", `colum ...

  6. 123data路径

    /zhf/Innosky2021/data/dayantahttp://123.xx.xx.147:8888/cesiumdemo/data/dayanta/tileset.json

  7. 根据pid定时监控CPU使用率和内存使用率并输出到文件 (windows和linux跨平台可用)

    有时服务器运维中,某些程序员的应用发布后完全不管CPU和内存的使用率,只觉得代码能运行就行了,这样给我们运维人员经常造成困扰: 比如我在zabbix平台中就经常监测到凌晨1~3~5点时候突然CPU飙升 ...

  8. 三步建立自己域名的主页,Github Pages功能简明手册

    [task]把自己的页面上传到git上,用github pages功能托管网页. 准备工作: 1.自己的网页文件 2.有个自己的git账号 3.下载安装git.下载地址https://git-scm. ...

  9. 暴雪、迪士尼大佬用什么画画?RayLink远控软件助力解锁远程创作

    CG绘画从业者,如原画师.插画师.漫画家.设计师等,一定对数位板\数位屏不陌生,数位板\数位屏是完成CG绘画作品的重要绘图工具之一. 从画画小白到数字绘画大神,从0基础插画培训班学生到国际知名游戏.动 ...

  10. vvvvvvue

    <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="" ...